一、C++思维导图:
二、C++:
三、注释代码
1> 配置文件:.pro文件
QT += core gui
# 引入的类库,core表示核心库 gui图形化界面库greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# 超过版本4的qt,会自动加widgetsCONFIG += c++11
# 支持C++11版本# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# 简单警告不报错# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0# 管理工程文件
SOURCES += \ # 管理的是源文件main.cpp \mywnd.cppHEADERS += \ # 管理的是头文件mywnd.hFORMS += \ # 管理的是ui文件mywnd.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2> 头文件
#ifndef MYWND_H
#define MYWND_H //防止头文件重复包含#include <QMainWindow> //将父类的头文件引入
#include<QPushButton>//引入命名空间
QT_BEGIN_NAMESPACE
namespace Ui { class MyWnd; } //将ui界面拖拽生成的类引入
QT_END_NAMESPACE//自定义的界面类,公共继承自QMainWindow
class MyWnd : public QMainWindow
{Q_OBJECT //信号与槽的宏对象,没有该宏对象,信号与槽机制不能使用public:MyWnd(QWidget *parent = nullptr); //构造函数的声明~MyWnd(); //析构函数的声明private:Ui::MyWnd *ui; //定义一个指向ui界面上的指针变量QPushButton *btn; //自定义组件的指针};
#endif // MYWND_H
3> 源文件
#include "mywnd.h" //引入自定义类的头文件
#include "ui_mywnd.h" //该头文件是ui界面生成的头文件//构造函数的定义
MyWnd::MyWnd(QWidget *parent): QMainWindow(parent) //在子类的构造函数的初始化列表中,显式调用父类的有参构造, ui(new Ui::MyWnd) //给类中指针成员实例化堆区空间
{ui->setupUi(this); //使用ui指针。给界面进行设置this->btn = new QPushButton; //自定义组件使用this指针去找this->btn->setParent(this);
// this->btn->resize(50,30);ui->pushButton->setText("点我点我"); //ui界面上拖拽的组件使用ui指针去找}//析构函数的定义
MyWnd::~MyWnd()
{delete ui; // 将指针指向的堆区空间释放
}
4> 主程序
#include "mywnd.h" //引入自定义类的头文件#include <QApplication> //引入应用程序的头文件int main(int argc, char *argv[])
{QApplication a(argc, argv); //使用用于程序类的有参构造,构造一个对象 aMyWnd w; //使用自定义的界面,实例化一个对象 使用的是无参构造函数w.show(); //调用自己的成员函数,将界面展示出来,该函数是由父类提供的return a.exec(); //a.exec是轮询等待界面上是否有事件产生,防止程序结束
}
四、QT思维导图