1.QT概述1
1.1 简介
跨平台C++图形用户界面应用程序框架。提供艺术级图形界面所需功能。
1.2 应用实例
- Linux之KDE桌面环境
- WPS office
- Skype
- Google Earth
- VLC
- Virtual Box
1.3 特点
- 跨平台
- 接口简单
- 简化内存回收机制
- 效率高,快速构建应用程序
- 良好社区氛围
- 嵌入式
2. 第一个QT程序
2.1 新建项目
名称不能有中文、空格,路径不能有中文
- QWidget 父类,什么都无; QMainWindow 窗口,菜单栏 ; QDialog 对话框
- 版本控制系统:SVN、VSS、GIT
- .pro工程文件、解决方案,类似vs的.sln文件
2.2 main函数介绍
/********
main.cpp
********/
#include "mywidget.h"
#include <QApplication> //包含应用程序的头文件。
//应用程序入口 、argc命令行变量的数量、argv命令行变量数组。
int main(int argc, char *argv[])
{
//应用程序对象,QT中有且仅有一个。
QApplication a(argc, argv);
myWidget w;//窗口对象,父类QWidget。
w.show(); //窗口对象默认不显,调用show方法。
return a.exec();//应用程序进入消息循环机机制,代码阻塞到当前行。
}
3. 便捷使用
3.1 命名规范
- 类名 首字母大写,单词之间首字母大写
- 函数名、变量名 首字母小写单词之间首字母大写
3.2 快捷键
- 注释 Ctrl + /
- 运行 Ctrl + r
- 编译 Ctrl + b
- 字体缩放 Ctrl + 滚轮
- 查找 Ctrl + f
- 整行移动 Ctrl+ shift + ↑ 或者↓
- 帮助文档 F1 、左侧按钮、 文档路径
- 自动对齐 Ctrl + i
- 同名之间 .h 和 .cpp切换 F4
4. 窗口控件
/************
mywidget.cpp
************/
#include "mywidget.h"
#include "QPushButton"
myWidget::myWidget(QWidget *parent)
: QWidget(parent)
{
//创建一个按钮
QPushButton *btn=new QPushButton;
//btn->show();以顶层方式显示。
//依赖窗口显示
btn->setParent(this);
//显示文本
btn->setText("Push Me!");
//按照控件大小创建窗口
QPushButton *btn2=new QPushButton("第二个按钮",this);
//移动btn2按钮
btn2->move(100,50);
//设置窗口大小
resize(600,400);
//固定窗口大小
setFixedSize(600,400);
//设置窗口标题
setWindowTitle("第一个窗口");
}
myWidget::~myWidget()
{
}
/***********
mywidget.h
***********/
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class myWidget : public QWidget
{
Q_OBJECT //宏,允许类中使用信号和槽的机制。
public:
myWidget(QWidget *parent = nullptr);//构造函数
~myWidget();
};
#endif // MYWIDGET_H
#-------------------------
#20200718_first.pro
#by obsessioner,2020-07-18
#
#-------------------------
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
TARGET=myappexe
TEMPLATE=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
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
传智播客B站教程 ↩︎