|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
引言
Qt是一个跨平台的C++图形用户界面应用程序开发框架,它不仅提供了创建图形界面所需的各种组件,还包括了数据库、网络、多线程、XML、OpenGL等众多功能模块。在Linux平台上,Qt是开发桌面应用程序的首选框架之一,KDE桌面环境就是基于Qt构建的。本文将带你从零开始,系统学习Linux平台下的Qt图形界面编程,逐步掌握核心技能,最终能够开发出专业级的桌面应用程序。
一、Qt入门基础
1.1 环境搭建
在Linux系统中安装Qt开发环境非常简单。以Ubuntu为例,你可以通过以下命令安装Qt5开发工具:
- sudo apt update
- sudo apt install qt5-default qtcreator qtdeclarative5-dev qtbase5-dev-tools
复制代码
安装完成后,你可以从应用程序菜单中启动Qt Creator,这是Qt官方提供的集成开发环境(IDE),集成了代码编辑器、可视化界面设计器、调试器和构建工具。
1.2 Qt基本概念
在开始编写Qt程序之前,需要了解几个基本概念:
• QObject:所有Qt对象的基类,提供了对象树、信号槽等核心功能。
• QWidget:所有用户界面对象的基类,是Qt窗口系统的基础。
• QApplication:管理GUI应用程序的控制流和主要设置。
• 信号与槽(Signals & Slots):Qt的核心机制,用于对象之间的通信。
• 布局管理(Layout Management):自动排列和管理界面上的控件。
1.3 第一个Qt程序
让我们创建一个简单的”Hello World”程序,体验Qt的基本开发流程。
在Qt Creator中创建一个新的Qt Widgets Application项目,然后修改main.cpp文件:
- #include <QApplication>
- #include <QLabel>
- #include <QWidget>
- #include <QVBoxLayout>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- // 创建主窗口
- QWidget window;
- window.setWindowTitle("Hello Qt");
- window.resize(300, 200);
-
- // 创建标签控件
- QLabel *label = new QLabel("Hello, World!");
- label->setAlignment(Qt::AlignCenter);
-
- // 创建布局并添加标签
- QVBoxLayout *layout = new QVBoxLayout;
- layout->addWidget(label);
-
- // 设置窗口的布局
- window.setLayout(layout);
-
- // 显示窗口
- window.show();
-
- return app.exec();
- }
复制代码
这个程序创建了一个简单的窗口,其中包含一个居中显示的”Hello, World!“标签。编译并运行这个程序,你将看到一个基本的GUI窗口。
二、Qt核心技能
2.1 信号与槽机制
信号与槽是Qt的核心特性,它允许对象之间的通信,是实现事件驱动编程的基础。当一个对象的状态发生变化时,它会发出一个信号;其他对象可以连接到这个信号,并通过槽函数来响应这个变化。
让我们通过一个简单的例子来理解信号与槽:
- #include <QApplication>
- #include <QPushButton>
- #include <QMessageBox>
- #include <QVBoxLayout>
- #include <QWidget>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QWidget window;
- window.setWindowTitle("信号与槽示例");
- window.resize(300, 200);
-
- QPushButton *button = new QPushButton("点击我");
-
- // 连接按钮的clicked信号到QMessageBox的静态槽函数
- QObject::connect(button, &QPushButton::clicked, []() {
- QMessageBox::information(nullptr, "消息", "按钮被点击了!");
- });
-
- QVBoxLayout *layout = new QVBoxLayout;
- layout->addWidget(button);
-
- window.setLayout(layout);
- window.show();
-
- return app.exec();
- }
复制代码
在这个例子中,当用户点击按钮时,按钮会发出clicked信号,我们连接到一个lambda表达式(作为槽函数),该函数会显示一个消息框。
2.2 布局管理
Qt提供了多种布局管理器,帮助开发者自动排列和管理界面上的控件。常用的布局管理器包括:
• QHBoxLayout:水平布局,将控件从左到右排列。
• QVBoxLayout:垂直布局,将控件从上到下排列。
• QGridLayout:网格布局,将控件排列在网格中。
• QFormLayout:表单布局,适用于标签和字段的排列。
下面是一个使用多种布局的示例:
- #include <QApplication>
- #include <QWidget>
- #include <QPushButton>
- #include <QLineEdit>
- #include <QLabel>
- #include <QVBoxLayout>
- #include <QHBoxLayout>
- #include <QGridLayout>
- #include <QGroupBox>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QWidget window;
- window.setWindowTitle("布局管理示例");
- window.resize(400, 300);
-
- // 主垂直布局
- QVBoxLayout *mainLayout = new QVBoxLayout;
-
- // 第一组:水平布局
- QGroupBox *horizontalGroup = new QGroupBox("水平布局");
- QHBoxLayout *hLayout = new QHBoxLayout;
-
- for (int i = 1; i <= 3; ++i) {
- QPushButton *button = new QPushButton(QString("按钮 %1").arg(i));
- hLayout->addWidget(button);
- }
-
- horizontalGroup->setLayout(hLayout);
- mainLayout->addWidget(horizontalGroup);
-
- // 第二组:网格布局
- QGroupBox *gridGroup = new QGroupBox("网格布局");
- QGridLayout *gLayout = new QGridLayout;
-
- gLayout->addWidget(new QLabel("用户名:"), 0, 0);
- gLayout->addWidget(new QLineEdit(), 0, 1);
- gLayout->addWidget(new QLabel("密码:"), 1, 0);
- gLayout->addWidget(new QLineEdit(), 1, 1);
-
- QPushButton *loginButton = new QPushButton("登录");
- gLayout->addWidget(loginButton, 2, 0, 1, 2);
-
- gridGroup->setLayout(gLayout);
- mainLayout->addWidget(gridGroup);
-
- window.setLayout(mainLayout);
- window.show();
-
- return app.exec();
- }
复制代码
这个示例展示了如何在窗口中使用水平布局和网格布局来组织控件。
2.3 常用控件
Qt提供了丰富的标准控件,下面介绍一些常用的控件及其基本用法:
- #include <QApplication>
- #include <QWidget>
- #include <QPushButton>
- #include <QCheckBox>
- #include <QRadioButton>
- #include <QButtonGroup>
- #include <QVBoxLayout>
- #include <QDebug>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QWidget window;
- window.setWindowTitle("按钮控件示例");
-
- QVBoxLayout *layout = new QVBoxLayout;
-
- // 普通按钮
- QPushButton *pushButton = new QPushButton("普通按钮");
- QObject::connect(pushButton, &QPushButton::clicked, []() {
- qDebug() << "普通按钮被点击";
- });
- layout->addWidget(pushButton);
-
- // 复选框
- QCheckBox *checkBox = new QCheckBox("复选框");
- QObject::connect(checkBox, &QCheckBox::stateChanged, [](int state) {
- qDebug() << "复选框状态改变:" << (state == Qt::Checked ? "选中" : "未选中");
- });
- layout->addWidget(checkBox);
-
- // 单选按钮
- QButtonGroup *radioGroup = new QButtonGroup;
- QRadioButton *radio1 = new QRadioButton("选项1");
- QRadioButton *radio2 = new QRadioButton("选项2");
- radioGroup->addButton(radio1);
- radioGroup->addButton(radio2);
-
- QObject::connect(radioGroup, QOverload<QAbstractButton*>::of(&QButtonGroup::buttonClicked), [](QAbstractButton *button) {
- qDebug() << "单选按钮被点击:" << button->text();
- });
-
- layout->addWidget(radio1);
- layout->addWidget(radio2);
-
- window.setLayout(layout);
- window.show();
-
- return app.exec();
- }
复制代码- #include <QApplication>
- #include <QWidget>
- #include <QLineEdit>
- #include <QTextEdit>
- #include <QSpinBox>
- #include <QComboBox>
- #include <QSlider>
- #include <QLabel>
- #include <QVBoxLayout>
- #include <QDebug>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QWidget window;
- window.setWindowTitle("输入控件示例");
-
- QVBoxLayout *layout = new QVBoxLayout;
-
- // 单行文本输入
- QLineEdit *lineEdit = new QLineEdit;
- lineEdit->setPlaceholderText("请输入文本");
- QObject::connect(lineEdit, &QLineEdit::textChanged, [](const QString &text) {
- qDebug() << "文本改变:" << text;
- });
- layout->addWidget(new QLabel("单行文本输入:"));
- layout->addWidget(lineEdit);
-
- // 多行文本输入
- QTextEdit *textEdit = new QTextEdit;
- textEdit->setPlaceholderText("请输入多行文本");
- layout->addWidget(new QLabel("多行文本输入:"));
- layout->addWidget(textEdit);
-
- // 数字输入
- QSpinBox *spinBox = new QSpinBox;
- spinBox->setRange(0, 100);
- spinBox->setValue(50);
- layout->addWidget(new QLabel("数字输入:"));
- layout->addWidget(spinBox);
-
- // 下拉框
- QComboBox *comboBox = new QComboBox;
- comboBox->addItem("选项1");
- comboBox->addItem("选项2");
- comboBox->addItem("选项3");
- layout->addWidget(new QLabel("下拉框:"));
- layout->addWidget(comboBox);
-
- // 滑块
- QSlider *slider = new QSlider(Qt::Horizontal);
- slider->setRange(0, 100);
- slider->setValue(50);
- layout->addWidget(new QLabel("滑块:"));
- layout->addWidget(slider);
-
- window.setLayout(layout);
- window.show();
-
- return app.exec();
- }
复制代码- #include <QApplication>
- #include <QWidget>
- #include <QLabel>
- #include <QProgressBar>
- #include <QLCDNumber>
- #include <QPixmap>
- #include <QVBoxLayout>
- #include <QTimer>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QWidget window;
- window.setWindowTitle("显示控件示例");
-
- QVBoxLayout *layout = new QVBoxLayout;
-
- // 标签
- QLabel *label = new QLabel("这是一个标签");
- label->setAlignment(Qt::AlignCenter);
- layout->addWidget(label);
-
- // 进度条
- QProgressBar *progressBar = new QProgressBar;
- progressBar->setRange(0, 100);
- progressBar->setValue(0);
- layout->addWidget(new QLabel("进度条:"));
- layout->addWidget(progressBar);
-
- // LCD数字显示
- QLCDNumber *lcdNumber = new QLCDNumber;
- lcdNumber->setSegmentStyle(QLCDNumber::Flat);
- lcdNumber->display(0);
- layout->addWidget(new QLabel("LCD数字:"));
- layout->addWidget(lcdNumber);
-
- // 图片显示
- QLabel *imageLabel = new QLabel;
- QPixmap pixmap(":/images/sample.png"); // 假设有一个资源文件
- if (!pixmap.isNull()) {
- imageLabel->setPixmap(pixmap.scaled(200, 200, Qt::KeepAspectRatio));
- imageLabel->setAlignment(Qt::AlignCenter);
- } else {
- imageLabel->setText("无法加载图片");
- imageLabel->setAlignment(Qt::AlignCenter);
- }
- layout->addWidget(new QLabel("图片显示:"));
- layout->addWidget(imageLabel);
-
- // 设置定时器更新进度条和LCD数字
- QTimer timer;
- int value = 0;
- QObject::connect(&timer, &QTimer::timeout, [&]() {
- value = (value + 1) % 101;
- progressBar->setValue(value);
- lcdNumber->display(value);
- });
- timer.start(100);
-
- window.setLayout(layout);
- window.show();
-
- return app.exec();
- }
复制代码
三、Qt进阶主题
3.1 自定义控件
虽然Qt提供了丰富的标准控件,但有时我们需要创建自定义控件来满足特定需求。自定义控件通常通过继承现有的Qt控件或直接继承QWidget来实现。
下面是一个自定义圆形进度条的例子:
- // circularprogressbar.h
- #ifndef CIRCULARPROGRESSBAR_H
- #define CIRCULARPROGRESSBAR_H
- #include <QWidget>
- #include <QPainter>
- class CircularProgressBar : public QWidget
- {
- Q_OBJECT
- Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
- Q_PROPERTY(int minimum READ minimum WRITE setMinimum)
- Q_PROPERTY(int maximum READ maximum WRITE setMaximum)
- public:
- explicit CircularProgressBar(QWidget *parent = nullptr);
-
- int value() const;
- int minimum() const;
- int maximum() const;
-
- public slots:
- void setValue(int value);
- void setMinimum(int min);
- void setMaximum(int max);
-
- signals:
- void valueChanged(int value);
-
- protected:
- void paintEvent(QPaintEvent *event) override;
-
- private:
- int m_value;
- int m_minimum;
- int m_maximum;
- };
- #endif // CIRCULARPROGRESSBAR_H
复制代码- // circularprogressbar.cpp
- #include "circularprogressbar.h"
- #include <QPainter>
- #include <QTimer>
- CircularProgressBar::CircularProgressBar(QWidget *parent) : QWidget(parent),
- m_value(0),
- m_minimum(0),
- m_maximum(100)
- {
- setMinimumSize(100, 100);
- }
- int CircularProgressBar::value() const
- {
- return m_value;
- }
- int CircularProgressBar::minimum() const
- {
- return m_minimum;
- }
- int CircularProgressBar::maximum() const
- {
- return m_maximum;
- }
- void CircularProgressBar::setValue(int value)
- {
- if (m_value != value) {
- m_value = qBound(m_minimum, value, m_maximum);
- update(); // 触发重绘
- emit valueChanged(m_value);
- }
- }
- void CircularProgressBar::setMinimum(int min)
- {
- if (m_minimum != min) {
- m_minimum = min;
- if (m_value < m_minimum) {
- setValue(m_minimum);
- }
- update();
- }
- }
- void CircularProgressBar::setMaximum(int max)
- {
- if (m_maximum != max) {
- m_maximum = max;
- if (m_value > m_maximum) {
- setValue(m_maximum);
- }
- update();
- }
- }
- void CircularProgressBar::paintEvent(QPaintEvent *event)
- {
- Q_UNUSED(event);
-
- QPainter painter(this);
- painter.setRenderHint(QPainter::Antialiasing);
-
- // 计算绘制区域
- int side = qMin(width(), height());
- painter.setViewport((width() - side) / 2, (height() - side) / 2, side, side);
- painter.setWindow(0, 0, 100, 100);
-
- // 绘制背景圆环
- painter.setPen(QPen(Qt::lightGray, 5, Qt::SolidLine, Qt::RoundCap));
- painter.drawArc(10, 10, 80, 80, 30 * 16, 390 * 16);
-
- // 计算进度角度
- double percentage = static_cast<double>(m_value - m_minimum) / (m_maximum - m_minimum);
- int progressAngle = static_cast<int>(percentage * 390);
-
- // 绘制进度圆环
- painter.setPen(QPen(Qt::blue, 5, Qt::SolidLine, Qt::RoundCap));
- painter.drawArc(10, 10, 80, 80, 30 * 16, progressAngle * 16);
-
- // 绘制进度文本
- painter.setPen(Qt::black);
- QFont font = painter.font();
- font.setBold(true);
- font.setPointSize(10);
- painter.setFont(font);
- painter.drawText(0, 0, 100, 100, Qt::AlignCenter,
- QString("%1%").arg(static_cast<int>(percentage * 100)));
- }
复制代码
使用自定义控件的示例:
- #include <QApplication>
- #include <QWidget>
- #include <QVBoxLayout>
- #include <QPushButton>
- #include "circularprogressbar.h"
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QWidget window;
- window.setWindowTitle("自定义控件示例");
- window.resize(300, 400);
-
- QVBoxLayout *layout = new QVBoxLayout;
-
- // 创建自定义圆形进度条
- CircularProgressBar *progressBar = new CircularProgressBar;
- progressBar->setMinimum(0);
- progressBar->setMaximum(100);
- progressBar->setValue(30);
- layout->addWidget(progressBar);
-
- // 添加按钮来改变进度
- QPushButton *button = new QPushButton("增加进度");
- layout->addWidget(button);
-
- // 连接按钮点击信号到进度条
- QObject::connect(button, &QPushButton::clicked, [progressBar]() {
- int newValue = progressBar->value() + 10;
- if (newValue > progressBar->maximum()) {
- newValue = progressBar->minimum();
- }
- progressBar->setValue(newValue);
- });
-
- window.setLayout(layout);
- window.show();
-
- return app.exec();
- }
复制代码
3.2 模型视图编程
Qt的模型/视图架构是一种用于显示和编辑数据的方式,它将数据存储(模型)和数据表示(视图)分离。这种架构使得同一份数据可以用不同的方式显示,并且不需要修改数据源代码。
下面是一个使用模型/视图架构显示表格数据的例子:
- #include <QApplication>
- #include <QTableView>
- #include <QStandardItemModel>
- #include <QHeaderView>
- #include <QVBoxLayout>
- #include <QWidget>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QWidget window;
- window.setWindowTitle("模型视图编程示例");
- window.resize(600, 400);
-
- QVBoxLayout *layout = new QVBoxLayout;
-
- // 创建标准项模型
- QStandardItemModel *model = new QStandardItemModel(&window);
- model->setColumnCount(3);
- model->setRowCount(5);
-
- // 设置表头
- model->setHorizontalHeaderLabels({"姓名", "年龄", "职业"});
-
- // 填充数据
- QList<QString> names = {"张三", "李四", "王五", "赵六", "钱七"};
- QList<int> ages = {25, 30, 35, 28, 32};
- QList<QString> jobs = {"工程师", "医生", "教师", "律师", "设计师"};
-
- for (int row = 0; row < 5; ++row) {
- QStandardItem *nameItem = new QStandardItem(names[row]);
- QStandardItem *ageItem = new QStandardItem(QString::number(ages[row]));
- QStandardItem *jobItem = new QStandardItem(jobs[row]);
-
- model->setItem(row, 0, nameItem);
- model->setItem(row, 1, ageItem);
- model->setItem(row, 2, jobItem);
- }
-
- // 创建表格视图并设置模型
- QTableView *tableView = new QTableView;
- tableView->setModel(model);
-
- // 设置表头属性
- tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
- tableView->verticalHeader()->setVisible(false);
-
- // 设置选择行为
- tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
- tableView->setSelectionMode(QAbstractItemView::SingleSelection);
-
- layout->addWidget(tableView);
- window.setLayout(layout);
- window.show();
-
- return app.exec();
- }
复制代码
3.3 多线程编程
在GUI应用程序中,耗时的操作(如文件读写、网络请求、复杂计算等)如果在主线程中执行,会导致界面冻结,影响用户体验。Qt提供了QThread类和相关的类来支持多线程编程。
下面是一个使用多线程执行耗时任务的例子:
- // worker.h
- #ifndef WORKER_H
- #define WORKER_H
- #include <QObject>
- #include <QRunnable>
- class Worker : public QObject, public QRunnable
- {
- Q_OBJECT
- public:
- explicit Worker(QObject *parent = nullptr);
-
- void run() override;
-
- signals:
- void progressChanged(int value);
- void finished(const QString &result);
-
- public slots:
- void cancel();
-
- private:
- bool m_cancelled;
- };
- #endif // WORKER_H
复制代码- // worker.cpp
- #include "worker.h"
- #include <QThread>
- #include <QDebug>
- Worker::Worker(QObject *parent) : QObject(parent), m_cancelled(false)
- {
- setAutoDelete(true); // 任务完成后自动删除
- }
- void Worker::run()
- {
- QString result;
-
- for (int i = 0; i <= 100; ++i) {
- if (m_cancelled) {
- result = "任务被取消";
- break;
- }
-
- // 模拟耗时操作
- QThread::msleep(50);
-
- // 发送进度信号
- emit progressChanged(i);
-
- // 构建结果字符串
- if (i == 100) {
- result = "任务完成";
- }
- }
-
- // 发送完成信号
- emit finished(result);
- }
- void Worker::cancel()
- {
- m_cancelled = true;
- }
复制代码- // main.cpp
- #include <QApplication>
- #include <QWidget>
- #include <QVBoxLayout>
- #include <QPushButton>
- #include <QProgressBar>
- #include <QLabel>
- #include <QThreadPool>
- #include "worker.h"
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- QWidget window;
- window.setWindowTitle("多线程编程示例");
- window.resize(400, 200);
-
- QVBoxLayout *layout = new QVBoxLayout;
-
- QProgressBar *progressBar = new QProgressBar;
- progressBar->setRange(0, 100);
- progressBar->setValue(0);
-
- QLabel *statusLabel = new QLabel("准备就绪");
-
- QPushButton *startButton = new QPushButton("开始任务");
- QPushButton *cancelButton = new QPushButton("取消任务");
- cancelButton->setEnabled(false);
-
- Worker *worker = nullptr;
-
- QObject::connect(startButton, &QPushButton::clicked, [&]() {
- // 创建工作线程
- worker = new Worker;
-
- // 连接信号槽
- QObject::connect(worker, &Worker::progressChanged, progressBar, &QProgressBar::setValue);
- QObject::connect(worker, &Worker::finished, [=](const QString &result) {
- statusLabel->setText(result);
- startButton->setEnabled(true);
- cancelButton->setEnabled(false);
- });
-
- // 启动线程
- QThreadPool::globalInstance()->start(worker);
-
- // 更新UI状态
- startButton->setEnabled(false);
- cancelButton->setEnabled(true);
- statusLabel->setText("任务进行中...");
- });
-
- QObject::connect(cancelButton, &QPushButton::clicked, [&]() {
- if (worker) {
- worker->cancel();
- }
- });
-
- layout->addWidget(progressBar);
- layout->addWidget(statusLabel);
- layout->addWidget(startButton);
- layout->addWidget(cancelButton);
-
- window.setLayout(layout);
- window.show();
-
- return app.exec();
- }
复制代码
这个例子中,我们在工作线程中执行一个模拟的耗时任务,并通过信号槽机制与主线程的UI进行通信,避免了界面冻结的问题。
四、实战案例:文本编辑器
现在,让我们综合运用前面所学的知识,开发一个简单的文本编辑器应用程序。这个文本编辑器将具有以下功能:
1. 新建、打开、保存文件
2. 基本的文本编辑功能
3. 查找和替换文本
4. 字体设置
5. 状态栏显示信息
4.1 项目结构
首先,创建一个Qt Widgets Application项目,然后添加以下文件:
• mainwindow.h:主窗口类的头文件
• mainwindow.cpp:主窗口类的实现文件
• textedit.h:自定义文本编辑类的头文件
• textedit.cpp:自定义文本编辑类的实现文件
• finddialog.h:查找对话框的头文件
• finddialog.cpp:查找对话框的实现文件
• replacedialog.h:替换对话框的头文件
• replacedialog.cpp:替换对话框的实现文件
4.2 自定义文本编辑类
- // textedit.h
- #ifndef TEXTEDIT_H
- #define TEXTEDIT_H
- #include <QTextEdit>
- class TextEdit : public QTextEdit
- {
- Q_OBJECT
- public:
- explicit TextEdit(QWidget *parent = nullptr);
-
- bool loadFile(const QString &fileName);
- bool saveFile(const QString &fileName);
- QString currentFile() const;
-
- protected:
- void closeEvent(QCloseEvent *event) override;
-
- private slots:
- void documentWasModified();
-
- private:
- void setCurrentFile(const QString &fileName);
- bool maybeSave();
-
- QString curFile;
- };
- #endif // TEXTEDIT_H
复制代码- // textedit.cpp
- #include "textedit.h"
- #include <QCloseEvent>
- #include <QFile>
- #include <QTextStream>
- #include <QMessageBox>
- #include <QApplication>
- TextEdit::TextEdit(QWidget *parent) : QTextEdit(parent)
- {
- connect(document(), &QTextDocument::contentsChanged,
- this, &TextEdit::documentWasModified);
- setWindowModified(false);
- }
- bool TextEdit::loadFile(const QString &fileName)
- {
- QFile file(fileName);
- if (!file.open(QFile::ReadOnly | QFile::Text)) {
- QMessageBox::warning(this, tr("文本编辑器"),
- tr("无法读取文件 %1:\n%2.")
- .arg(QDir::toNativeSeparators(fileName),
- file.errorString()));
- return false;
- }
- QTextStream in(&file);
- QApplication::setOverrideCursor(Qt::WaitCursor);
- setPlainText(in.readAll());
- QApplication::restoreOverrideCursor();
- setCurrentFile(fileName);
- return true;
- }
- bool TextEdit::saveFile(const QString &fileName)
- {
- QFile file(fileName);
- if (!file.open(QFile::WriteOnly | QFile::Text)) {
- QMessageBox::warning(this, tr("文本编辑器"),
- tr("无法写入文件 %1:\n%2.")
- .arg(QDir::toNativeSeparators(fileName),
- file.errorString()));
- return false;
- }
- QTextStream out(&file);
- QApplication::setOverrideCursor(Qt::WaitCursor);
- out << toPlainText();
- QApplication::restoreOverrideCursor();
- setCurrentFile(fileName);
- return true;
- }
- QString TextEdit::currentFile() const
- {
- return curFile;
- }
- void TextEdit::closeEvent(QCloseEvent *event)
- {
- if (maybeSave()) {
- event->accept();
- } else {
- event->ignore();
- }
- }
- void TextEdit::documentWasModified()
- {
- setWindowModified(document()->isModified());
- }
- void TextEdit::setCurrentFile(const QString &fileName)
- {
- curFile = fileName;
- document()->setModified(false);
- setWindowModified(false);
- QString shownName;
- if (curFile.isEmpty())
- shownName = "untitled.txt";
- else
- shownName = QFileInfo(curFile).fileName();
- setWindowTitle(tr("%1[*] - %2").arg(shownName, QCoreApplication::applicationName()));
- }
- bool TextEdit::maybeSave()
- {
- if (!document()->isModified())
- return true;
- const QMessageBox::StandardButton ret
- = QMessageBox::warning(this, tr("文本编辑器"),
- tr("文档已被修改.\n"
- "是否保存修改?"),
- QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
- switch (ret) {
- case QMessageBox::Save:
- return true;
- case QMessageBox::Cancel:
- return false;
- default:
- break;
- }
- return true;
- }
复制代码
4.3 查找对话框
- // finddialog.h
- #ifndef FINDDIALOG_H
- #define FINDDIALOG_H
- #include <QDialog>
- class QCheckBox;
- class QLabel;
- class QLineEdit;
- class QPushButton;
- class FindDialog : public QDialog
- {
- Q_OBJECT
- public:
- explicit FindDialog(QWidget *parent = nullptr);
-
- QString getFindText() const;
- bool caseSensitive() const;
- bool wholeWords() const;
- bool backward() const;
-
- signals:
- void findNext(const QString &str, Qt::CaseSensitivity cs);
- void findPrevious(const QString &str, Qt::CaseSensitivity cs);
- private slots:
- void findClicked();
- void enableFindButton(const QString &text);
- private:
- QLineEdit *lineEdit;
- QCheckBox *caseCheckBox;
- QCheckBox *wholeWordsCheckBox;
- QCheckBox *backwardCheckBox;
- QPushButton *findButton;
- QPushButton *closeButton;
- };
- #endif // FINDDIALOG_H
复制代码- // finddialog.cpp
- #include "finddialog.h"
- #include <QLabel>
- #include <QLineEdit>
- #include <QCheckBox>
- #include <QPushButton>
- #include <QHBoxLayout>
- #include <QVBoxLayout>
- FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
- {
- QLabel *label = new QLabel(tr("查找内容:"));
- lineEdit = new QLineEdit;
- label->setBuddy(lineEdit);
-
- caseCheckBox = new QCheckBox(tr("区分大小写"));
- wholeWordsCheckBox = new QCheckBox(tr("全词匹配"));
- backwardCheckBox = new QCheckBox(tr("向上查找"));
-
- findButton = new QPushButton(tr("查找"));
- findButton->setDefault(true);
- findButton->setEnabled(false);
-
- closeButton = new QPushButton(tr("关闭"));
-
- connect(lineEdit, &QLineEdit::textChanged, this, &FindDialog::enableFindButton);
- connect(findButton, &QPushButton::clicked, this, &FindDialog::findClicked);
- connect(closeButton, &QPushButton::clicked, this, &FindDialog::close);
-
- QHBoxLayout *topLeftLayout = new QHBoxLayout;
- topLeftLayout->addWidget(label);
- topLeftLayout->addWidget(lineEdit);
-
- QVBoxLayout *leftLayout = new QVBoxLayout;
- leftLayout->addLayout(topLeftLayout);
- leftLayout->addWidget(caseCheckBox);
- leftLayout->addWidget(wholeWordsCheckBox);
- leftLayout->addWidget(backwardCheckBox);
-
- QVBoxLayout *rightLayout = new QVBoxLayout;
- rightLayout->addWidget(findButton);
- rightLayout->addWidget(closeButton);
- rightLayout->addStretch();
-
- QHBoxLayout *mainLayout = new QHBoxLayout;
- mainLayout->addLayout(leftLayout);
- mainLayout->addLayout(rightLayout);
- setLayout(mainLayout);
-
- setWindowTitle(tr("查找"));
- setFixedHeight(sizeHint().height());
- }
- QString FindDialog::getFindText() const
- {
- return lineEdit->text();
- }
- bool FindDialog::caseSensitive() const
- {
- return caseCheckBox->isChecked();
- }
- bool FindDialog::wholeWords() const
- {
- return wholeWordsCheckBox->isChecked();
- }
- bool FindDialog::backward() const
- {
- return backwardCheckBox->isChecked();
- }
- void FindDialog::findClicked()
- {
- Qt::CaseSensitivity cs = caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
-
- if (backward()) {
- emit findPrevious(getFindText(), cs);
- } else {
- emit findNext(getFindText(), cs);
- }
- }
- void FindDialog::enableFindButton(const QString &text)
- {
- findButton->setEnabled(!text.isEmpty());
- }
复制代码
4.4 替换对话框
- // replacedialog.h
- #ifndef REPLACEDIALOG_H
- #define REPLACEDIALOG_H
- #include <QDialog>
- class QCheckBox;
- class QLabel;
- class QLineEdit;
- class QPushButton;
- class ReplaceDialog : public QDialog
- {
- Q_OBJECT
- public:
- explicit ReplaceDialog(QWidget *parent = nullptr);
-
- QString getFindText() const;
- QString getReplaceText() const;
- bool caseSensitive() const;
- bool wholeWords() const;
-
- signals:
- void findNext(const QString &str, Qt::CaseSensitivity cs);
- void replace(const QString &findText, const QString &replaceText, Qt::CaseSensitivity cs);
- void replaceAll(const QString &findText, const QString &replaceText, Qt::CaseSensitivity cs);
- private slots:
- void findClicked();
- void replaceClicked();
- void replaceAllClicked();
- void enableFindButton(const QString &text);
- private:
- QLineEdit *findLineEdit;
- QLineEdit *replaceLineEdit;
- QCheckBox *caseCheckBox;
- QCheckBox *wholeWordsCheckBox;
- QPushButton *findButton;
- QPushButton *replaceButton;
- QPushButton *replaceAllButton;
- QPushButton *closeButton;
- };
- #endif // REPLACEDIALOG_H
复制代码- // replacedialog.cpp
- #include "replacedialog.h"
- #include <QLabel>
- #include <QLineEdit>
- #include <QCheckBox>
- #include <QPushButton>
- #include <QHBoxLayout>
- #include <QVBoxLayout>
- ReplaceDialog::ReplaceDialog(QWidget *parent) : QDialog(parent)
- {
- QLabel *findLabel = new QLabel(tr("查找内容:"));
- findLineEdit = new QLineEdit;
- findLabel->setBuddy(findLineEdit);
-
- QLabel *replaceLabel = new QLabel(tr("替换为:"));
- replaceLineEdit = new QLineEdit;
- replaceLabel->setBuddy(replaceLineEdit);
-
- caseCheckBox = new QCheckBox(tr("区分大小写"));
- wholeWordsCheckBox = new QCheckBox(tr("全词匹配"));
-
- findButton = new QPushButton(tr("查找"));
- findButton->setDefault(true);
- findButton->setEnabled(false);
-
- replaceButton = new QPushButton(tr("替换"));
- replaceButton->setEnabled(false);
-
- replaceAllButton = new QPushButton(tr("全部替换"));
- replaceAllButton->setEnabled(false);
-
- closeButton = new QPushButton(tr("关闭"));
-
- connect(findLineEdit, &QLineEdit::textChanged, this, &ReplaceDialog::enableFindButton);
- connect(findButton, &QPushButton::clicked, this, &ReplaceDialog::findClicked);
- connect(replaceButton, &QPushButton::clicked, this, &ReplaceDialog::replaceClicked);
- connect(replaceAllButton, &QPushButton::clicked, this, &ReplaceDialog::replaceAllClicked);
- connect(closeButton, &QPushButton::clicked, this, &ReplaceDialog::close);
-
- QHBoxLayout *findLayout = new QHBoxLayout;
- findLayout->addWidget(findLabel);
- findLayout->addWidget(findLineEdit);
-
- QHBoxLayout *replaceLayout = new QHBoxLayout;
- replaceLayout->addWidget(replaceLabel);
- replaceLayout->addWidget(replaceLineEdit);
-
- QVBoxLayout *leftLayout = new QVBoxLayout;
- leftLayout->addLayout(findLayout);
- leftLayout->addLayout(replaceLayout);
- leftLayout->addWidget(caseCheckBox);
- leftLayout->addWidget(wholeWordsCheckBox);
-
- QVBoxLayout *rightLayout = new QVBoxLayout;
- rightLayout->addWidget(findButton);
- rightLayout->addWidget(replaceButton);
- rightLayout->addWidget(replaceAllButton);
- rightLayout->addWidget(closeButton);
- rightLayout->addStretch();
-
- QHBoxLayout *mainLayout = new QHBoxLayout;
- mainLayout->addLayout(leftLayout);
- mainLayout->addLayout(rightLayout);
- setLayout(mainLayout);
-
- setWindowTitle(tr("替换"));
- setFixedHeight(sizeHint().height());
- }
- QString ReplaceDialog::getFindText() const
- {
- return findLineEdit->text();
- }
- QString ReplaceDialog::getReplaceText() const
- {
- return replaceLineEdit->text();
- }
- bool ReplaceDialog::caseSensitive() const
- {
- return caseCheckBox->isChecked();
- }
- bool ReplaceDialog::wholeWords() const
- {
- return wholeWordsCheckBox->isChecked();
- }
- void ReplaceDialog::findClicked()
- {
- Qt::CaseSensitivity cs = caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
- emit findNext(getFindText(), cs);
- }
- void ReplaceDialog::replaceClicked()
- {
- Qt::CaseSensitivity cs = caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
- emit replace(getFindText(), getReplaceText(), cs);
- }
- void ReplaceDialog::replaceAllClicked()
- {
- Qt::CaseSensitivity cs = caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
- emit replaceAll(getFindText(), getReplaceText(), cs);
- }
- void ReplaceDialog::enableFindButton(const QString &text)
- {
- findButton->setEnabled(!text.isEmpty());
- replaceButton->setEnabled(!text.isEmpty());
- replaceAllButton->setEnabled(!text.isEmpty());
- }
复制代码
4.5 主窗口
- // mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include "textedit.h"
- class QAction;
- class QMenu;
- class QStatusBar;
- class FindDialog;
- class ReplaceDialog;
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- MainWindow();
- protected:
- void closeEvent(QCloseEvent *event) override;
- private slots:
- void newFile();
- void open();
- bool save();
- bool saveAs();
- void about();
- void documentWasModified();
- void find();
- void replace();
- void findNext(const QString &str, Qt::CaseSensitivity cs);
- void findPrevious(const QString &str, Qt::CaseSensitivity cs);
- void replaceText(const QString &findText, const QString &replaceText, Qt::CaseSensitivity cs);
- void replaceAllText(const QString &findText, const QString &replaceText, Qt::CaseSensitivity cs);
- void fontDialog();
- private:
- void createActions();
- void createMenus();
- void createStatusBar();
- void readSettings();
- void writeSettings();
- bool maybeSave();
- void loadFile(const QString &fileName);
- bool saveFile(const QString &fileName);
- void setCurrentFile(const QString &fileName);
- QString strippedName(const QString &fullFileName);
- TextEdit *textEdit;
- FindDialog *findDialog;
- ReplaceDialog *replaceDialog;
-
- QMenu *fileMenu;
- QMenu *editMenu;
- QMenu *formatMenu;
- QMenu *helpMenu;
-
- QAction *newAct;
- QAction *openAct;
- QAction *saveAct;
- QAction *saveAsAct;
- QAction *exitAct;
- QAction *undoAct;
- QAction *redoAct;
- QAction *cutAct;
- QAction *copyAct;
- QAction *pasteAct;
- QAction *findAct;
- QAction *replaceAct;
- QAction *fontAct;
- QAction *aboutAct;
- QAction *aboutQtAct;
-
- QString curFile;
- };
- #endif // MAINWINDOW_H
复制代码- // mainwindow.cpp
- #include "mainwindow.h"
- #include "finddialog.h"
- #include "replacedialog.h"
- #include <QApplication>
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QTextStream>
- #include <QFontDialog>
- #include <QStatusBar>
- #include <QCloseEvent>
- MainWindow::MainWindow()
- {
- textEdit = new TextEdit;
- setCentralWidget(textEdit);
-
- findDialog = nullptr;
- replaceDialog = nullptr;
-
- createActions();
- createMenus();
- createStatusBar();
-
- readSettings();
-
- setUnifiedTitleAndToolBarOnMac(true);
- }
- void MainWindow::closeEvent(QCloseEvent *event)
- {
- if (textEdit->maybeSave()) {
- writeSettings();
- event->accept();
- } else {
- event->ignore();
- }
- }
- void MainWindow::newFile()
- {
- if (textEdit->maybeSave()) {
- textEdit->clear();
- setCurrentFile(QString());
- }
- }
- void MainWindow::open()
- {
- if (textEdit->maybeSave()) {
- QString fileName = QFileDialog::getOpenFileName(this);
- if (!fileName.isEmpty())
- loadFile(fileName);
- }
- }
- bool MainWindow::save()
- {
- if (curFile.isEmpty()) {
- return saveAs();
- } else {
- return saveFile(curFile);
- }
- }
- bool MainWindow::saveAs()
- {
- QString fileName = QFileDialog::getSaveFileName(this);
- if (fileName.isEmpty())
- return false;
- return saveFile(fileName);
- }
- void MainWindow::about()
- {
- QMessageBox::about(this, tr("关于文本编辑器"),
- tr("<b>文本编辑器</b> 是一个使用Qt开发的基本文本编辑应用程序。"
- "它可以用来编辑和保存纯文本文件。"));
- }
- void MainWindow::documentWasModified()
- {
- setWindowModified(textEdit->document()->isModified());
- }
- void MainWindow::find()
- {
- if (!findDialog) {
- findDialog = new FindDialog(this);
- connect(findDialog, &FindDialog::findNext, this, &MainWindow::findNext);
- connect(findDialog, &FindDialog::findPrevious, this, &MainWindow::findPrevious);
- }
-
- findDialog->show();
- findDialog->raise();
- findDialog->activateWindow();
- }
- void MainWindow::replace()
- {
- if (!replaceDialog) {
- replaceDialog = new ReplaceDialog(this);
- connect(replaceDialog, &ReplaceDialog::findNext, this, &MainWindow::findNext);
- connect(replaceDialog, &ReplaceDialog::replace, this, &MainWindow::replaceText);
- connect(replaceDialog, &ReplaceDialog::replaceAll, this, &MainWindow::replaceAllText);
- }
-
- replaceDialog->show();
- replaceDialog->raise();
- replaceDialog->activateWindow();
- }
- void MainWindow::findNext(const QString &str, Qt::CaseSensitivity cs)
- {
- if (!textEdit->find(str, QTextDocument::FindFlag(0) | (cs == Qt::CaseSensitive ? QTextDocument::FindCaseSensitively : QTextDocument::FindFlag(0)))) {
- QMessageBox::information(this, tr("查找"), tr("找不到 "%1"").arg(str));
- }
- }
- void MainWindow::findPrevious(const QString &str, Qt::CaseSensitivity cs)
- {
- if (!textEdit->find(str, QTextDocument::FindBackward | (cs == Qt::CaseSensitive ? QTextDocument::FindCaseSensitively : QTextDocument::FindFlag(0)))) {
- QMessageBox::information(this, tr("查找"), tr("找不到 "%1"").arg(str));
- }
- }
- void MainWindow::replaceText(const QString &findText, const QString &replaceText, Qt::CaseSensitivity cs)
- {
- QTextCursor cursor = textEdit->textCursor();
- if (!cursor.hasSelection() || cursor.selectedText() != findText) {
- if (!textEdit->find(findText, QTextDocument::FindFlag(0) | (cs == Qt::CaseSensitive ? QTextDocument::FindCaseSensitively : QTextDocument::FindFlag(0)))) {
- QMessageBox::information(this, tr("替换"), tr("找不到 "%1"").arg(findText));
- return;
- }
- }
-
- textEdit->textCursor().insertText(replaceText);
- }
- void MainWindow::replaceAllText(const QString &findText, const QString &replaceText, Qt::CaseSensitivity cs)
- {
- QTextDocument::FindFlags flags;
- if (cs == Qt::CaseSensitive)
- flags |= QTextDocument::FindCaseSensitively;
-
- int count = 0;
- QTextCursor cursor = textEdit->textCursor();
- cursor.movePosition(QTextCursor::Start);
- textEdit->setTextCursor(cursor);
-
- while (textEdit->find(findText, flags)) {
- textEdit->textCursor().insertText(replaceText);
- count++;
- }
-
- QMessageBox::information(this, tr("替换"), tr("已替换 %1 处").arg(count));
- }
- void MainWindow::fontDialog()
- {
- bool ok;
- QFont font = QFontDialog::getFont(&ok, textEdit->font(), this);
- if (ok) {
- textEdit->setFont(font);
- }
- }
- void MainWindow::createActions()
- {
- newAct = new QAction(tr("新建"), this);
- newAct->setShortcuts(QKeySequence::New);
- newAct->setStatusTip(tr("创建新文件"));
- connect(newAct, &QAction::triggered, this, &MainWindow::newFile);
- openAct = new QAction(tr("打开..."), this);
- openAct->setShortcuts(QKeySequence::Open);
- openAct->setStatusTip(tr("打开已有文件"));
- connect(openAct, &QAction::triggered, this, &MainWindow::open);
- saveAct = new QAction(tr("保存"), this);
- saveAct->setShortcuts(QKeySequence::Save);
- saveAct->setStatusTip(tr("保存当前文档"));
- connect(saveAct, &QAction::triggered, this, &MainWindow::save);
- saveAsAct = new QAction(tr("另存为..."), this);
- saveAsAct->setShortcuts(QKeySequence::SaveAs);
- saveAsAct->setStatusTip(tr("以新名称保存文档"));
- connect(saveAsAct, &QAction::triggered, this, &MainWindow::saveAs);
- exitAct = new QAction(tr("退出"), this);
- exitAct->setShortcuts(QKeySequence::Quit);
- exitAct->setStatusTip(tr("退出应用程序"));
- connect(exitAct, &QAction::triggered, this, &QWidget::close);
- undoAct = new QAction(tr("撤销"), this);
- undoAct->setShortcuts(QKeySequence::Undo);
- connect(undoAct, &QAction::triggered, textEdit, &TextEdit::undo);
- redoAct = new QAction(tr("重做"), this);
- redoAct->setShortcuts(QKeySequence::Redo);
- connect(redoAct, &QAction::triggered, textEdit, &TextEdit::redo);
- cutAct = new QAction(tr("剪切"), this);
- cutAct->setShortcuts(QKeySequence::Cut);
- connect(cutAct, &QAction::triggered, textEdit, &TextEdit::cut);
- copyAct = new QAction(tr("复制"), this);
- copyAct->setShortcuts(QKeySequence::Copy);
- connect(copyAct, &QAction::triggered, textEdit, &TextEdit::copy);
- pasteAct = new QAction(tr("粘贴"), this);
- pasteAct->setShortcuts(QKeySequence::Paste);
- connect(pasteAct, &QAction::triggered, textEdit, &TextEdit::paste);
- findAct = new QAction(tr("查找..."), this);
- findAct->setShortcuts(QKeySequence::Find);
- connect(findAct, &QAction::triggered, this, &MainWindow::find);
- replaceAct = new QAction(tr("替换..."), this);
- replaceAct->setShortcuts(QKeySequence::Replace);
- connect(replaceAct, &QAction::triggered, this, &MainWindow::replace);
- fontAct = new QAction(tr("字体..."), this);
- connect(fontAct, &QAction::triggered, this, &MainWindow::fontDialog);
- aboutAct = new QAction(tr("关于"), this);
- aboutAct->setStatusTip(tr("显示应用程序的关于框"));
- connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
- aboutQtAct = new QAction(tr("关于 Qt"), this);
- aboutQtAct->setStatusTip(tr("显示Qt库的关于框"));
- connect(aboutQtAct, &QAction::triggered, qApp, &QApplication::aboutQt);
- }
- void MainWindow::createMenus()
- {
- fileMenu = menuBar()->addMenu(tr("文件"));
- fileMenu->addAction(newAct);
- fileMenu->addAction(openAct);
- fileMenu->addAction(saveAct);
- fileMenu->addAction(saveAsAct);
- fileMenu->addSeparator();
- fileMenu->addAction(exitAct);
- editMenu = menuBar()->addMenu(tr("编辑"));
- editMenu->addAction(undoAct);
- editMenu->addAction(redoAct);
- editMenu->addSeparator();
- editMenu->addAction(cutAct);
- editMenu->addAction(copyAct);
- editMenu->addAction(pasteAct);
- editMenu->addSeparator();
- editMenu->addAction(findAct);
- editMenu->addAction(replaceAct);
- formatMenu = menuBar()->addMenu(tr("格式"));
- formatMenu->addAction(fontAct);
- helpMenu = menuBar()->addMenu(tr("帮助"));
- helpMenu->addAction(aboutAct);
- helpMenu->addAction(aboutQtAct);
- }
- void MainWindow::createStatusBar()
- {
- statusBar()->showMessage(tr("就绪"));
- }
- void MainWindow::readSettings()
- {
- QSettings settings("MyCompany", "TextEditor");
- QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
- QSize size = settings.value("size", QSize(400, 400)).toSize();
- resize(size);
- move(pos);
- }
- void MainWindow::writeSettings()
- {
- QSettings settings("MyCompany", "TextEditor");
- settings.setValue("pos", pos());
- settings.setValue("size", size());
- }
- bool MainWindow::maybeSave()
- {
- if (!textEdit->document()->isModified())
- return true;
- const QMessageBox::StandardButton ret
- = QMessageBox::warning(this, tr("文本编辑器"),
- tr("文档已被修改.\n"
- "是否保存修改?"),
- QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
- switch (ret) {
- case QMessageBox::Save:
- return save();
- case QMessageBox::Cancel:
- return false;
- default:
- break;
- }
- return true;
- }
- void MainWindow::loadFile(const QString &fileName)
- {
- if (!textEdit->loadFile(fileName)) {
- statusBar()->showMessage(tr("加载失败"), 2000);
- return;
- }
- setCurrentFile(fileName);
- statusBar()->showMessage(tr("文件已加载"), 2000);
- }
- bool MainWindow::saveFile(const QString &fileName)
- {
- if (!textEdit->saveFile(fileName)) {
- statusBar()->showMessage(tr("保存失败"), 2000);
- return false;
- }
- setCurrentFile(fileName);
- statusBar()->showMessage(tr("文件已保存"), 2000);
- return true;
- }
- void MainWindow::setCurrentFile(const QString &fileName)
- {
- curFile = fileName;
- textEdit->document()->setModified(false);
- setWindowModified(false);
- QString shownName;
- if (curFile.isEmpty())
- shownName = "untitled.txt";
- else
- shownName = QFileInfo(curFile).fileName();
- setWindowTitle(tr("%1[*] - %2").arg(shownName, QCoreApplication::applicationName()));
- }
- QString MainWindow::strippedName(const QString &fullFileName)
- {
- return QFileInfo(fullFileName).fileName();
- }
复制代码
4.6 主函数
- // main.cpp
- #include "mainwindow.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- app.setApplicationName("文本编辑器");
- app.setOrganizationName("MyCompany");
-
- MainWindow window;
- window.show();
-
- return app.exec();
- }
复制代码
4.7 项目文件
- # TextEditor.pro
- QT += core gui
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
- CONFIG += c++17
- # You can make your code fail to compile if it uses deprecated APIs.
- # In order to do so, uncomment the following line.
- #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
- SOURCES += \
- main.cpp \
- mainwindow.cpp \
- textedit.cpp \
- finddialog.cpp \
- replacedialog.cpp
- HEADERS += \
- mainwindow.h \
- textedit.h \
- finddialog.h \
- replacedialog.h
- # Default rules for deployment.
- qnx: target.path = /tmp/$${TARGET}/bin
- else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
复制代码
这个文本编辑器应用程序具有完整的文件操作功能(新建、打开、保存、另存为)、基本的文本编辑功能(撤销、重做、剪切、复制、粘贴)、查找和替换功能以及字体设置功能。通过这个实战案例,我们综合运用了前面所学的Qt核心技能,包括信号槽机制、布局管理、常用控件、对话框、文件操作等。
五、常见问题与解决方案
5.1 中文显示乱码问题
在Linux平台下,Qt应用程序中显示中文时可能会出现乱码问题。这通常是由于编码不一致导致的。解决方法如下:
- #include <QApplication>
- #include <QTextCodec>
- #include <QLabel>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
-
- // 设置编码为UTF-8
- QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
-
- QLabel label("你好,世界!");
- label.show();
-
- return app.exec();
- }
复制代码
另外,在Qt Creator中,确保源代码文件以UTF-8编码保存。可以在”工具” -> “选项” -> “文本编辑器” -> “行为”中设置默认编码为UTF-8。
5.2 程序打包与发布
在Linux平台下,Qt应用程序的打包与发布通常有以下几种方式:
静态编译将Qt库直接链接到可执行文件中,使得程序可以在没有安装Qt库的系统上运行。但是,静态编译需要自己编译Qt库,并且会增加可执行文件的大小。
- # 在.pro文件中添加以下配置
- CONFIG += static
复制代码
linuxdeployqt是一个用于打包Qt应用程序的工具,它会收集所有依赖的库并创建一个AppDir或AppImage。
- # 安装linuxdeployqt
- wget -c "https://github.com/probonopd/linuxdeployqt/releases/download/continuous/linuxdeployqt-continuous-x86_64.AppImage"
- chmod a+x linuxdeployqt-continuous-x86_64.AppImage
- # 打包应用程序
- ./linuxdeployqt-continuous-x86_64.AppImage /path/to/your/app -appimage
复制代码
如果你的目标系统是基于Debian的(如Ubuntu),可以创建Debian包来分发应用程序。
- # 安装打包工具
- sudo apt install dh-make devscripts
- # 创建Debian包结构
- dh_make --createorig --single --yes
- # 构建包
- debuild -us -uc
复制代码
5.3 性能优化
Qt应用程序的性能优化是一个重要的话题,以下是一些常见的优化技巧:
- // 在自定义控件中,只有当内容真正改变时才调用update()
- void MyWidget::setData(const Data &data)
- {
- if (m_data != data) {
- m_data = data;
- update(); // 触发重绘
- }
- }
复制代码
当需要显示大量数据时,使用模型/视图架构比直接使用控件更高效:
- // 使用QTableView和自定义模型而不是QTableWidget
- QTableView *tableView = new QTableView;
- MyCustomModel *model = new MyCustomModel(this);
- tableView->setModel(model);
复制代码- // 将耗时操作放在工作线程中
- class Worker : public QObject
- {
- Q_OBJECT
- public:
- explicit Worker(QObject *parent = nullptr) : QObject(parent) {}
-
- public slots:
- void doWork()
- {
- // 执行耗时操作
- emit resultReady(result);
- }
-
- signals:
- void resultReady(const Result &result);
- };
- // 在主线程中
- QThread *thread = new QThread;
- Worker *worker = new Worker;
- worker->moveToThread(thread);
- connect(thread, &QThread::started, worker, &Worker::doWork);
- connect(worker, &Worker::resultReady, this, &MyClass::handleResult);
- connect(worker, &Worker::resultReady, thread, &QThread::quit);
- connect(worker, &Worker::resultReady, worker, &Worker::deleteLater);
- connect(thread, &QThread::finished, thread, &QThread::deleteLater);
- thread->start();
复制代码
5.4 内存管理
Qt的父子对象机制简化了内存管理,但在某些情况下仍需特别注意:
- // 错误示例:没有设置父对象,可能导致内存泄漏
- void MyClass::createWidgets()
- {
- QPushButton *button = new QPushButton("Click me");
- // 没有设置父对象,也没有手动删除
- }
- // 正确示例:设置父对象
- void MyClass::createWidgets()
- {
- QPushButton *button = new QPushButton("Click me", this); // 设置父对象
- // 父对象被删除时,会自动删除子对象
- }
复制代码- #include <QSharedPointer>
- #include <QWeakPointer>
- // 使用QSharedPointer管理对象
- QSharedPointer<MyClass> sharedObj(new MyClass);
- // 使用QWeakPointer避免循环引用
- class ClassA : public QObject
- {
- Q_OBJECT
- public:
- void setClassB(QWeakPointer<ClassB> b) { m_b = b; }
- private:
- QWeakPointer<ClassB> m_b;
- };
- class ClassB : public QObject
- {
- Q_OBJECT
- public:
- void setClassA(QSharedPointer<ClassA> a) { m_a = a; }
- private:
- QSharedPointer<ClassA> m_a;
- };
复制代码
六、学习路径与资源推荐
6.1 学习路径
对于初学者,建议按照以下路径学习Qt编程:
1. 基础阶段:学习C++基础知识了解Qt的基本概念和架构掌握信号槽机制学习常用控件的使用理解布局管理
2. 学习C++基础知识
3. 了解Qt的基本概念和架构
4. 掌握信号槽机制
5. 学习常用控件的使用
6. 理解布局管理
7. 进阶阶段:学习自定义控件的创建掌握模型/视图编程了解Qt的多线程编程学习文件操作和数据处理掌握Qt的绘图系统
8. 学习自定义控件的创建
9. 掌握模型/视图编程
10. 了解Qt的多线程编程
11. 学习文件操作和数据处理
12. 掌握Qt的绘图系统
13. 高级阶段:学习Qt的网络编程掌握Qt的数据库操作了解Qt的国际化支持学习Qt的单元测试掌握应用程序的打包与发布
14. 学习Qt的网络编程
15. 掌握Qt的数据库操作
16. 了解Qt的国际化支持
17. 学习Qt的单元测试
18. 掌握应用程序的打包与发布
基础阶段:
• 学习C++基础知识
• 了解Qt的基本概念和架构
• 掌握信号槽机制
• 学习常用控件的使用
• 理解布局管理
进阶阶段:
• 学习自定义控件的创建
• 掌握模型/视图编程
• 了解Qt的多线程编程
• 学习文件操作和数据处理
• 掌握Qt的绘图系统
高级阶段:
• 学习Qt的网络编程
• 掌握Qt的数据库操作
• 了解Qt的国际化支持
• 学习Qt的单元测试
• 掌握应用程序的打包与发布
6.2 推荐资源
• Qt官方文档:最权威的Qt参考资料,包含详细的类说明和示例代码。
• Qt Wiki:包含各种教程、技巧和最佳实践。
• Qt示例代码:官方提供的示例代码,涵盖各种功能和使用场景。
• 《C++ GUI Programming with Qt 4》:经典的Qt编程书籍,虽然基于Qt 4,但大部分内容仍然适用。
• 《Qt 5开发实战》:详细介绍Qt 5的各种功能和编程技巧。
• 《Qt Creator快速入门》:专注于Qt Creator IDE的使用和Qt编程基础。
• Qt中文社区:国内最大的Qt开发者社区,有丰富的教程和讨论。
• Stack Overflow:全球最大的编程问答网站,有大量Qt相关的问题和解答。
• GitHub上的Qt项目:可以学习其他开发者的代码和项目。
• Qt官方YouTube频道:官方发布的各种教程和演示视频。
• Bilibili上的Qt教程:国内视频网站上有许多Qt相关的中文教程。
结语
Qt是一个功能强大、灵活且跨平台的GUI开发框架,特别适合在Linux平台上开发桌面应用程序。通过本文的学习,你已经从入门到精通掌握了Qt图形界面编程的核心技能,包括基本概念、常用控件、信号槽机制、布局管理、自定义控件、模型视图编程、多线程编程等内容。通过实战案例的开发,你了解了如何综合运用这些技能来构建一个完整的桌面应用程序。同时,我们还讨论了常见问题的解决方案和学习资源推荐,帮助你进一步提高Qt编程能力。
Qt的学习是一个持续的过程,随着Qt版本的更新和新功能的加入,你需要不断学习和探索。希望本文能够成为你Qt编程之路上的指南,帮助你开发出更加专业、高效的桌面应用程序。 |
|