初学Qt,Qt应用程序开发框架
QT助手:QT中所包含的类、函数等的查询工具
xx:\Qt\Qt5.9.8\5.9.8\mingw53_32\bin\assistant.exe
一、创建Qt项目
1、 启动目录
安装QT后,无法找到QT启动的位置,启动qtcreator.exe,目录如下:
安装的盘符:\Qt\Qt5.9.8\Tools\QtCreator\bin\qtcreator.exe
2、启动QT后,新建项目
(1)点击创建项目按钮
(2)选择模板(Application 应用、QT Widgets Application QT空窗体应用程序)
(3)填写项目名称及选择项目路径
(4)编译器选择,如果之前安装时选择VS的,可以在此处选择VS2017或2015或2013等
(5)选择类,Widget为空窗口,MainWindow (主窗口)和Dialog(对话框)是Widget的派生类
MainWindow中包含菜单栏、工具栏、状态栏
Dialog包含确定及取消按钮
(6)类名首字母大写
(7)版本控制系统(代码同步工具),例如SVN、GIT
二、Qt项目创建后,各个文件详细内容记录
1、xxx.pro文件 项目文件
#-------------------------------------------------
# 创建项目名称 及 创建项目时间
# Project created by QtCreator 2021-02-19T12:58:28
#
#-------------------------------------------------
# 为QT添加模块 core QT核心模块 gui 图形用户界面模块
QT += core gui
#4.0以后的版本 添加widgets模块
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#生成的.exe应用程序 名字
TARGET = QTCreateProjectOne
#使用的模板
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.
#定义选项添加 QT宏 QT_DEPRECATED_WARNINGS当API过时时,编译器提示警告
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
#支持C++ll新特性 实时更新
CONFIG += c++11
SOURCES += \
main.cpp \
Widget.cpp
HEADERS += \
Widget.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2、Qt各个模块及功能概况
3、头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>//包含 Qt空窗口头文件
class Widget : public QWidget
{
Q_OBJECT//支持信号 和 槽
public:
Widget(QWidget *parent = 0);//析构函数
~Widget();//析构函数
};
#endif // WIDGET_H
4、Main函数 类文件
main.cpp
#include "Widget.h"
#include <QApplication>//应用程序头文件
/*
* QT编辑器快捷键
* 注释 ctrl+/
* 转到定义或声明切换 F2
* 上一步、下一步 alt+← alt+→
*
* 运行 ctrl+R
* 构建 ctrl+B
* */
int main(int argc, char *argv[])
{
QApplication a(argc, argv);//定义应用程序对象
Widget w;//定义空窗口对象
w.show();//调用空窗口子函数,显示窗口
return a.exec();//调用应用程序对象 消息循环函数
}
Widget.cpp
#include "Widget.h"
//构造函数
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//记忆接口
this->setWindowTitle("设置窗口标题");//设置窗口标题
this->resize(400, 500);//设置窗口大小,宽、高
this->setFixedSize(200, 200);//设置固定窗口大小,窗口不能拖拽改变大小
}
//析构函数
Widget::~Widget()
{
}