初学QT,使用QT Creator建立了一个基类窗口项目,以下文件是IDE自动生成
项目文件注解(xxxxx.pro):
#QT有很多基本模块,常用的为QT Widgets
#QT Core和QT GUI是内部的基本模块
#注释项目文件需要使用‘#’
#模块
#头文件按F1可以查询模块所在地
#仅仅只包含头文件还不行,还需要加入相应模块
#不是很懂
QT += core gui
#高于4版本,添加QT+=widgets,为了兼容QT4
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += 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
#TARGET = xxxx xxxx是应用程序的名字(初始为项目的名字)
#TEMPLATE = xxx xxx指定makefile的类型,app(可是其他,目前不涉及到这方面)
# 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
#源文件.cpp文件
SOURCES += \
main.cpp \
myfirstwidget.cpp
#头文件.h文件
HEADERS += \
myfirstwidget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
头文件注解(xxxxx.h):
#ifndef MYFIRSTWIDGET_H
#define MYFIRSTWIDGET_H
#include <QWidget>
class MyfirstWidget : public QWidget
{
Q_OBJECT//信号与槽的时候需要
public:
MyfirstWidget(QWidget *parent = nullptr);
~MyfirstWidget();
};
#endif // MYFIRSTWIDGET_H
主函数文件注解(main.cpp):
#include "myfirstwidget.h"
//QApplication是应用程序类
//QT头文件没有.h
//头文件和类名一样
#include <QApplication>
int main(int argc, char *argv[])
{
//有且只有一个应用程序类的对象
QApplication a(argc, argv);
//MyfirstWidget继承QWidget,QWidget是一个窗口基类
//所以MyfirstWidget也是窗口类
//w是一个窗口
MyfirstWidget w;
//窗口默认隐藏,需要人为显示
w.show();
//a.exec()的作用
//让程序一直执行,等待用户操作
//等待事件的发生
return a.exec();
}
窗口类实现文件注解(xxxxx.cpp):
#include "myfirstwidget.h"
MyfirstWidget::MyfirstWidget(QWidget *parent)
: QWidget(parent)
{
}
MyfirstWidget::~MyfirstWidget()
{
}