QT 是一个跨平台的C++图形用户界面应用程序框架。
这篇文章能教会你简单的使用QT。
1.初步了解
2.进一步了解
3.信号和槽
4.实现两个窗口的连接
1.初步了解:
创建项目:
(Widgets是部件的意思)Widgets Application 部件应用程序
(文件路径不能存在中文)
基类:
MainWindow 主要用于PC端(windows,带菜单栏)
QWidget 控件的基类
QDialog 对话框
了解一下:
父类 || 子类
基类 || 派生类
会生成一个 class MyWidget : public QWidget (MyWidget继承窗口基类QWidget)
接着下一步,完成
编译运行的方法:
图中的箭头。
快捷键: ctrl+r
.pro为项目文件
#-------------------------------------------------
#
# Project created by QtCreator 2019-06-04T18:33:58
#
#-------------------------------------------------
#模块,找模块,头文件按F1
QT += core gui
#为了兼容QT4以前的版本
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#应用程序的名字
TARGET = day_011
#指定makefile的类型,app,不用管
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
//源文件 .cpp文件
SOURCES += \
main.cpp \
mywidget.cpp
//头文件 .h文件
HEADERS += \
mywidget.h
右键点击.pro文件→在Explorer中显示
自动生成build…
.h头文件
会生成类MyWidget
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class MyWidget : public QWidget //光标对着QWidget,按F1跳到官方对应的文档
{
Q_OBJECT //构造的函数,按F4可以看到对应的.cpp是空的
//信号与槽的时候需要
public:
MyWidget(QWidget *parent = 0);
~MyWidget();
};
#endif // MYWIDGET_H
main.cpp 主函数
#include "mywidget.h"
//QApplication应用程序类
#include <QApplication>
int main(int argc, char *argv[])
{
//有且只有一个应用程序类的对象
QApplication a(argc, argv);
//MyWidget继承于QWidget,QWidget是一个窗口基类
//所以MyWidget也是一个窗口类
//w就是一个窗口
MyWidget w;
//窗口默认是隐藏的,要显示出来
w.show();
//等于a.exec();return 0;
//作用让程序一直执行,等待用户操作
//等待事件发生
return a.exec();
}
主要框架:
#include <QApplication>
int main(int argc, char *argv[