UI文件设计与运行机制
项目文件组成
QT += core gui //表示项目中加入core gui模块 (GUI设计的类库模块)
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets//Qt主版本大于4加入widgets模块
CONFIG += c++11
# 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 \
widget.cpp
HEADERS += \ //头文件
widget.h
FORMS += \ //窗体文件
widget.ui //.ui对窗体进行可视化设计
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
1.组件面板
2.待设计窗体
3.布局和界面设计工具栏:Signals和Slots编辑器用于可视化进行信号与槽的关联,Action编辑器用于可视化设计Action
3.对象浏览器:窗口右上方
4.属性编辑器:窗口右下方
QLabel继承关系
QObject-QWidget-QFrame-QLabel
widget.ui文件
是窗体界面定义文件,是一个XML文件,定义了窗口上所有组件的属性设置,布局,信号与槽函数的关联,秩序在ui设计器里进行可视化设计,不用管这个
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>90</x>
<y>40</y>
<width>80</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>Hello Word</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
ui_widget.h文件
出现在编译后的目录
Signal&Slot
QObject::connect(sender,SIGNAL(signal()),receiver,SLOT(slot()));
//connect()是QObject类的一个静态函数,QObject是所有Qt类的基类,在实际调用时可以忽略前面的限定符
connect(sender,SIGNAL(signal()),receiver,SLOT(slot()));
//
//信号和槽拓展
1.信号连接信号
2.一个信号可以连接多个函数
3.多个信号可以连接同一个槽函数
4.信号和槽的参数类型,必须一一对应
5.信号的参数个数可以多于槽函数,反之不可以,相同个数的参数类型也要一一对应
6.可以利用disconnect 断开信号槽的连接
使用sender()信号发射者
QObject::sender()可以获取信号发射者的指针