一、Qt的背景
关于客户端开发:
客户端开发的重要任务,是编写和用户交互的界面,和用户交互的界面有两种风格:
- TUI:命令行界面,也叫终端界面
- GUI:图形化界面
Qt就是用来编写给普通用户使用的图形化界面。
概念:Qt是一个跨平台的C++图形用户界面应用程序框架
Qt支持的系统:
- windows
- Linux
- Mac系统
- 嵌入式系统
Linux主要给服务器使用,服务器不需要图形界面
TUI:基于命令操作,门槛高。效率高;
GUI:基于图形操作,门槛低,效率低
Qt的优点:
- 跨平台,几乎支持所有平台
- 接口简单,容易上手
- 一定程度上简化了内存回收机制(半自动的垃圾回收,简化内存释放,尽可能小的影响程序的效率)
- 开发效率高,能够快速构建应用程序
- 可以进行嵌入式开发
Qt的应用场景:
- 桌面应用程序
- 移动应用程序
- 嵌入式系统
二、认识Qt项目
刚创建的Qt项目有以下文件:
先来认识下main.cpp文件:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
- main函数的形参:是命令行参数
- QApplication a(argc, argv);:编写Qt的图形化界面,必须要有QApplication 对象
- Widget w;:创建项目时生成的类名
- w.show();:创建一个控件对象,并显示出来(.show()显示,.hide()隐藏)
- Widget 的父类是QWidget ,方法都是这个父类提供的
widget.h文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
- Q_OBJECT:是一个Qt内置的宏,宏本质是文本替换,宏展开时会生成一堆代码。Qt中有一个非常核心的机制,信号和槽,如果某个类想使用信号和槽,必须引入这个宏
- QWidget *parent = nullptr:Qt中引入了对象树机制,创建的Qt对象,可以把这个对象挂到对象树上,对象树是一个N叉树
- Ui::Widget *ui;:和form file密切相关
widget.cpp文件:
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
- #include “widget.h”:创建项目生成的头文件
- #include “ui_widget.h”:form file被qmake生成的头文件
- ui(new Ui::Widget)和ui->setupUi(this);:把form file生成的界面和当前的widget关联起来
Forms file中的widget.ui文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
</widget>
<resources/>
<connections/>
</ui>
.ui文件的格式是xml,Qt中使用xml文件来描述程序的界面是什么样的,同时qmake工具会调用相关的工具,依据这个xml文件生成一些代码,从而把完整的界面构造出来
.pro文件:
QT += core gui
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
# 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 \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
.pro文件是Qt项目的工程文件,也是qmake工具构建时的重要依据
QT += core gui:表示要引入的Qt的模块
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
这段代码用来描述当前项目中参与构建的文件有哪些(编译器需要编译哪些文件,不需要我们手动修改)
.pro文件类似与Linux中的makefile文件,只是Qt中的“makefile”文件不需要我们自己写,而是qmake自动生成