Abel'Blog

我干了什么?究竟拿了时间换了什么?

0%

Qt

概述

QT的全称就叫做QT。Qt 是一个1991年由Qt Company开发的跨平台C++图形用户界面应用程序开发框架。它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器。

Qt是面向对象的框架,使用特殊的代码生成扩展以及一些宏,Qt很容易扩展,并且允许真正地组件编程。Qt支持下列操作系统: Microsoft Windows 95/98, Microsoft Windows NT, Linux, Solaris, SunOS, HP-UX, Digital UNIX Irix, FreeBSD, BSD/OS, SCO, AIX, OS390,QNX 等等。Qt 的良好封装机制使得 Qt 的模块化程度非常高,可重用性较好,对于用户开发来说是非常 方便的。并且Qt 提供了一种称为 signals/slots 的安全类型来替代 callback,这使得各个元件 之间的协同工作变得十分简单。

工程文件介绍

.pro

1
2
3
4
5
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

重点是qt含有这些模块。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Module
Description
Qt Core
Core non-graphical classes used by other modules.
Qt GUI
Base classes for graphical user interface (GUI) components. Includes OpenGL.
Qt Multimedia
Classes for audio, video, radio and camera functionality.
Qt Multimedia Widgets
Widget-based classes for implementing multimedia functionality.
Qt Network
Classes to make network programming easier and more portable.
Qt QML
Classes for QML and JavaScript languages.
Qt Quick
A declarative framework for building highly dynamic applications with custom user interfaces.
Qt Quick Controls
Provides lightweight QML types for creating performant user interfaces for desktop, embedded, and mobile devices. These types employ a simple styling architecture and are very efficient.
Qt Quick Dialogs
Types for creating and interacting with system dialogs from a Qt Quick application.
Qt Quick Layouts
Layouts are items that are used to arrange Qt Quick 2 based items in the user interface.
Qt Quick Test
A unit test framework for QML applications, where the test cases are written as JavaScript functions.
Note: The binary compatibility guarantee does not apply to Qt Quick Test. However, it will remain source compatible.
Qt SQL
Classes for database integration using SQL.
Qt Test
Classes for unit testing Qt applications and libraries.
Note: The binary compatibility guarantee does not apply to Qt Test. However, it will remain source compatible.
Qt Widgets
Classes to extend Qt GUI with C++ widgets.

对象树

在qt里面使用对象树来建立继承的层级关系,方便让其在释放的时候自动释放内存。

坐标系

最坐上位置为0,0,x往右。y往下。

信号槽

信号发送方,信号接收方是分离的,使用connect来将两个部分耦合。

示例在一个QWidget里面创建一个QPushButtom,通过手册找到QPushButtom的基类QAbstractButton文档中,Signals章节能找到它能支持的4个信号。我们选择clicked。信号函数就是&QPushButton::click

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 函数原型:
QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)

// 调用示例:
connect(ui->btnOpenMinor,&QPushButton::clicked,this, &MainWindow::on_manual_bind_clicked,Qt::ConnectionType::AutoConnection);

// 绑定的函数
void MainWindow::on_manual_bind_clicked()
{
qDebug("%s",__FUNCTION__);
}

void test() {

void someFunction();
QPushButton *button = new QPushButton;
QObject::connect(button, &QPushButton::clicked, someFunction);
}

// 自定义一个信号。信号定义者
class Teacher {
signals:
void hungry();
void hungry(QString food); // 重载了之后需要在链接链接的时候使用函数指针翻出来
}


// 槽部分代码:
class Student {
public slots:
void treat(); // 定义,并且需要实现;
void treat(QString);
}

// 调用部分的代码:
connect(teacher,&Teacher::hungry,study,&Student::treat);

void (Teacher:: *teacherSignal)(QString) = &Teacher::hungry; // 这样就能消除了二义性;
void (Student:: *student)(QString) = &Student::treat;
connect(teach,teacherSignal,study,student);

// 信号连接信号

// 可以考虑使用 connect 先连接到一个方法,然后调用 emit 函数将一个信号发出去。
emit zt->hungry("狗不理包子");
// 直接连接信号
void (Teacher:: *teacherSignal1)() = &Teacher::hungry;
connect(btnExit,QPushButton::clicked,teacher,&Teacher::teacherSignal1);

// 断开信号,参数和connect一致;
disdconnect(btnExit,QPushButton::clicked,teacher,&Teacher::teacherSignal1);

总结:

  • 信号可以连接多个槽函数;
  • 信号可以连接信号;
  • 多个信号能连接同一个槽函数;
  • 信号和槽函数需要参数表要对应;
  • 信号参数可以多一些,槽函数可以少于它提供的函数;
  • qt4的写法,可以直接使用宏来规定信号,槽函数 SIGNAL SLOT,可以直观的将函数列出来,但是无法检测到问题,出问题,也没有法子知道出问题了。如果槽函数参数和信号没有

资源文件加载

先将资源文件拷贝到项目目录中。

新建Qt Resource File创建一个qrc文件。然后就是添加前缀,将全部的图片资源导入。

在代码中使用资源的时候是:+前缀名+文件名就能访问这个美术资源了。

模态和非模态对话框

模态能共存,使用show()函数。

非模态只能有一个界面能用。

布局

垂直布局、水平布局、网格布局。

可以使用QWidget将一些空间打包成一个组,然后使用垂直布局、水平布局。

多屏幕支持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int screenIndex = m_desktop->primaryScreen();
qDebug()<<"屏幕的索引号"<<screenIndex;
int screenCount = m_desktop->screenCount();
qDebug()<<"屏幕的个数"<<screenCount;
QRect screenSize = m_desktop->screenGeometry();
qDebug()<<"屏幕的分辨率"<<screenSize;
int screenWidth = m_desktop->width();
qDebug()<<"屏幕的总宽度"<<screenWidth;
int screenHeight = m_desktop->height();
qDebug()<<"屏幕的高度"<<screenHeight;

m_desktop = QApplication::desktop();
//获取屏幕的索引号 0 代表主屏幕 1代表扩展屏
int screenIndex = m_desktop->primaryScreen();
//此时将这个软件作为主屏幕
QRect rect = m_desktop->screenGeometry(0);
this->setGeometry(rect);

视频播放相关

使用qt中的socket

参考