# 在此项目中加入core gui核心(控制台不需要)# QT是以模块的形式组织类库,根据项目需求不同添加不同类库的支持QT += core gui# 当QT版本大于4,在当前项目中添加widgetsgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.# In order to do so, uncomment the following line.#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0# 都为自动生成的# 源代码SOURCES += \ main.cpp \ widgets.cpp# 头文件HEADERS += \ widgets.h# ui文件FORMS += \ widgets.ui# Default rules for deployment.默认部署规则qnx: target.path = /tmp/$${TARGET}/binelse: unix:!android: target.path = /opt/$${TARGET}/bin!isEmpty(target.path): INSTALLS += target
#include "widgets.h"#include "ui_widgets.h"#include "testwidget.h"widgets::widgets(QWidget *parent) : QWidget(parent), ui(new Ui::widgets) //开辟类内存空间{ //调用setupUi, //通过此就可修改主窗口的相关设置于获取 ui->setupUi(this); //实现窗口的各种空间属性,信号与槽关联等 //一般在qt的构造函数中进行数据初始化操作(窗口,数据....) //如显示当前窗口,并显示另一个窗口 //要独立显示窗口,必须要进行show操作#if 0: TestWidget* w = new TestWidget; //显示当前窗体 w->show();# else // 指定了父对象,子窗口和父窗口一起显示出来,内嵌入父窗口 TestWidget* w = new TestWidget(this); //显示到this里面,testwidget被嵌入到this对应的主窗口#endig}widgets::~widgets(){ // 析构函数释放new的空间 delete ui;}
使用代码实现在widgets上的功能
QMainWindow带菜单栏的窗口
使用代码实现不创建UI相关文件
文件列表
可视化和代码编写对比:
可视化不用编写代码,极大的省去界面布局调节时间成本
但有些组件没法完全通过ui进行可视化添加需要设计纯代码
纯代码确实效率会低,繁琐,但可补充ui无法实现的功能
对象数Object Trees
QObjects organize themselves in object trees. When you create a QObject with another object as parent, it’s added to the parent’s children() list, and is deleted when the parent is. It turns out that this approach fits the needs of GUI objects very well. For example, a QShortcut (keyboard shortcut) is a child of the relevant window, so when the user closes that window, the shortcut is deleted too.
QQuickItem, the basic visual element of the Qt Quick module, inherits from QObject, but has a concept of the visual parent which differs from that of the QObject parent. An item’s visual parent may not necessarily be the same as its object parent. See Concepts - Visual Parent in Qt Quick for more details.
QWidget, the fundamental class of the Qt Widgets module, extends the parent-child relationship. A child normally also becomes a child widget, i.e. it is displayed in its parent’s coordinate system and is graphically clipped by its parent’s boundaries. For example, when the application deletes a message box after it has been closed, the message box’s buttons and label are also deleted, just as we’d want, because the buttons and label are children of the message box.
You can also delete child objects yourself, and they will remove themselves from their parents. For example, when the user removes a toolbar it may lead to the application deleting one of its QToolBar objects, in which case the tool bar’s QMainWindow parent would detect the change and reconfigure its screen space accordingly.
The debugging functions QObject::dumpObjectTree() and QObject::dumpObjectInfo() are often useful when an application looks or acts strangely.
QDebug
In the common case, it is useful to call the qDebug() function to obtain a default QDebug object to use for writing debugging information.