创建QT项目
前言
创建一个QT项目
一、如何创建新项目
点击创建项目后,选择项目路径以及给项目起名字。
名称不能有中文和空格
路径不能有中文路径
二、类信息
基类:QWidget、QMainWindow、QDialog。
QWidget:最简单窗口,即空窗口。
QMainWindow:含菜单栏、状态栏。
QDialog:对话框,一般有最小化按钮、选择按钮等。
QMainWindow和QDialog是QWidget的附属(前两者比后者包含更多东西)
三、文件分析
1.main.cpp
代码如下:
#include "mywidget.h"
#include <QApplication>//包含一个应用程序类的头文件
//main程序入口 argc命令行变量数量 argv命令行变量的数组
int main(int argc, char *argv[])
{
//a是应用程序对象,在QT中应用程序对象有且只有一个
QApplication a(argc, argv);
//窗口对象 myWidget的父类为QWidget
myWidget w;
//窗口对象默认不会显示,必须调用show函数显示窗口
w.show();
//让应用程序对象进入消息循环
//让代码阻塞到当前行并接收用户操作不会让窗口一闪而过,除非点击关闭叉号
return a.exec();
//以下代码和return a.exec()功能相同
// while(true)
// {
// if(点击窗口关闭的叉号)
// {
// break;
// }
// }
}
2.01_FirstProject.pro
代码如下(示例):
//QT包含的模块
QT += core gui
//QT版本大于4包含widgets模块
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
//目标 生成01_FirstProject.exe程序名称,若更改为02_SecondProject,则会对应生成02_SecondProject.exe文件
TARGET=01_FirstProject
//模板 在这为应用程序(application)模板
TEMPLATE=app
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
# 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 \
mywidget.cpp
//头文件
HEADERS += \
mywidget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
3.mywidget.h
代码如下:
#ifndef MYWIDGET_H
#define MYWIDGET_H
//如果没有头文件可以用#pragma once等效
#include <QWidget>//包含头文件 QWidght窗口类头文件
class myWidget : public QWidget//继承
{
Q_OBJECT//宏,允许类中使用信号和槽的机制
public:
myWidget(QWidget *parent = nullptr);//构造函数(有参构造,默认参数)
~myWidget();//析构函数
};
#endif // MYWIDGET_H
4.mywidget.cpp
代码如下:
#include "mywidget.h"
myWidget::myWidget(QWidget *parent)
: QWidget(parent)//成员初始化列表
{
}
myWidget::~myWidget()
{
}
四、命名规范
类名:首字母大写,单词单词之间首字母大写 函数名与变量名称:首字母小写,单词单词之间首字母大写五、快捷键
注释:Ctrl+/
运行:Ctrl+R
编译:Ctrl+B
查找:Ctrl+F
整行移动: Ctrl+Shift+键盘上键或者下键
自动对齐:Ctrl+I
同名文件.h和.cpp切换:F4
帮助文档:F1;左侧帮助;D:\QT\Qt\5.14.2\mingw73_32\bin
总结
老师的QT版本和自己的有出入。然后还发现自己C++学的……挺差的,可能还得补一补。在保姆级教学下学会了第一个QT项目创建,希望再接再厉叭(不会再咕。