跳到主要内容

Qt 知识速查表

常用类速查

核心类

类名功能头文件
QApplication应用程序入口<QApplication>
QObject所有 Qt 对象的基类<QObject>
QWidget所有 UI 控件的基类<QWidget>
QMainWindow主窗口<QMainWindow>
QDialog对话框基类<QDialog>

基础控件

类名功能头文件
QLabel标签<QLabel>
QPushButton按钮<QPushButton>
QLineEdit单行输入<QLineEdit>
QTextEdit多行文本<QTextEdit>
QComboBox下拉框<QComboBox>
QCheckBox复选框<QCheckBox>
QRadioButton单选按钮<QRadioButton>
QSlider滑块<QSlider>
QProgressBar进度条<QProgressBar>

高级控件

类名功能头文件
QListWidget列表<QListWidget>
QTableWidget表格<QTableWidget>
QTreeWidget树形<QTreeWidget>
QTabWidget标签页<QTabWidget>
QStackedWidget堆叠窗口<QStackedWidget>
QSplitter分割器<QSplitter>

布局类

类名功能头文件
QVBoxLayout垂直布局<QVBoxLayout>
QHBoxLayout水平布局<QHBoxLayout>
QGridLayout网格布局<QGridLayout>
QFormLayout表单布局<QFormLayout>

对话框

类名功能头文件
QMessageBox消息框<QMessageBox>
QFileDialog文件对话框<QFileDialog>
QInputDialog输入对话框<QInputDialog>
QColorDialog颜色对话框<QColorDialog>
QFontDialog字体对话框<QFontDialog>

网络类

类名功能头文件
QNetworkAccessManagerHTTP 管理<QNetworkAccessManager>
QTcpSocketTCP 客户端<QTcpSocket>
QTcpServerTCP 服务器<QTcpServer>
QUdpSocketUDP 通信<QUdpSocket>

信号槽连接方式

// 1. 传统语法(Qt 4)
connect(sender, SIGNAL(signal()), receiver, SLOT(slot()));

// 2. 新语法(Qt 5+,推荐)
connect(sender, &Sender::signal, receiver, &Receiver::slot);

// 3. Lambda 表达式
connect(sender, &Sender::signal, [=]() {
// 处理代码
});

// 4. 带连接类型
connect(sender, &Sender::signal, receiver, &Receiver::slot, Qt::QueuedConnection);

常用宏

用途
Q_OBJECT启用元对象系统(必须)
SIGNAL()标记信号(传统语法)
SLOT()标记槽(传统语法)
Q_PROPERTY()声明属性
Q_INVOKABLE允许从元对象系统调用
Q_SLOTS声明槽函数区域
Q_SIGNALS声明信号区域

常用枚举值

对齐方式

Qt::AlignLeft      // 左对齐
Qt::AlignRight // 右对齐
Qt::AlignCenter // 居中
Qt::AlignTop // 顶部对齐
Qt::AlignBottom // 底部对齐
Qt::AlignHCenter // 水平居中
Qt::AlignVCenter // 垂直居中

连接类型

Qt::AutoConnection          // 自动选择(默认)
Qt::DirectConnection // 直接调用
Qt::QueuedConnection // 队列连接(异步)
Qt::BlockingQueuedConnection // 阻塞队列连接
Qt::UniqueConnection // 唯一连接

窗口标志

Qt::Window                  // 普通窗口
Qt::Dialog // 对话框
Qt::Popup // 弹出窗口
Qt::Tool // 工具窗口
Qt::FramelessWindowHint // 无边框
Qt::WindowStaysOnTopHint // 置顶

CMakeLists.txt 模板

cmake_minimum_required(VERSION 3.16)
project(MyQtApp VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Network)

add_executable(MyQtApp
main.cpp
mainwindow.cpp
mainwindow.h
mainwindow.ui
resources.qrc
)

target_link_libraries(MyQtApp PRIVATE
Qt6::Core
Qt6::Gui
Qt6::Widgets
Qt6::Network
)

常用代码片段

显示消息框

QMessageBox::information(this, "标题", "内容");
QMessageBox::warning(this, "警告", "警告内容");
QMessageBox::critical(this, "错误", "错误内容");

if (QMessageBox::question(this, "确认", "确定删除?") == QMessageBox::Yes) {
// 执行删除
}

文件对话框

// 打开文件
QString file = QFileDialog::getOpenFileName(this, "打开", "", "文本 (*.txt)");

// 保存文件
QString file = QFileDialog::getSaveFileName(this, "保存", "", "文本 (*.txt)");

// 选择目录
QString dir = QFileDialog::getExistingDirectory(this, "选择目录");

定时器

// 单次定时器
QTimer::singleShot(1000, this, [=]() {
// 1秒后执行
});

// 重复定时器
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [=]() {
// 定时执行
});
timer->start(1000); // 每秒触发

字符串处理

QString str = "Hello Qt";
str.append("!");
str.prepend("Say: ");

// 格式化
QString formatted = QString("Name: %1, Age: %2").arg("张三").arg(25);

// 分割
QStringList parts = str.split(",");

// 替换
str.replace("old", "new");

容器遍历

// QList
QList<int> list;
for (int value : list) {
qDebug() << value;
}

// QMap
QMap<QString, int> map;
for (auto it = map.begin(); it != map.end(); ++it) {
qDebug() << it.key() << it.value();
}

// C++11 范围 for
for (const auto &key : map.keys()) {
qDebug() << key << map[key];
}

调试技巧

// 输出调试信息
qDebug() << "调试信息";
qWarning() << "警告信息";
qCritical() << "错误信息";

// 输出变量
int value = 42;
qDebug() << "value =" << value;

// 输出容器
QList<int> list = {1, 2, 3};
qDebug() << list;

官方文档链接