Qt6 学习——一个Qt桌面应用程序
文章目录
- Qt6 UI 指引
- 创建新项目
- CMakeLists.txt
- main.cpp
- mainwindow.cpp
- mainwindow.h
- mainwindow.ui
- 项目树形框架
- 运行结果
- 用qmake构建系统创建新项目
- 项目树形框架
- main.cpp
- mainwindow.cpp
- mainwindow.h
- 运行结果
Qt6 UI 指引









创建新项目










CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
project(forbetter LANGUAGES CXX)find_package(Qt6 6.5 REQUIRED COMPONENTS Core Widgets)qt_standard_project_setup()qt_add_executable(forbetterWIN32 MACOSX_BUNDLEmain.cppmainwindow.cppmainwindow.hmainwindow.ui
)target_link_libraries(forbetterPRIVATEQt::CoreQt::Widgets
)include(GNUInstallDirs)install(TARGETS forbetterBUNDLE DESTINATION .RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)qt_generate_deploy_app_script(TARGET forbetterOUTPUT_SCRIPT deploy_scriptNO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);// 创建了一个 MainWindow 类的实例 w,即应用程序的主窗口。MainWindow 类通常继承自 QMainWindow,并包含了应用程序的UI设计和功能逻辑MainWindow w;// 显示主窗口。show() 方法是 QWidget 类的成员函数,调用它会将窗口展示在屏幕上。w.show();// 进入Qt的事件循环,exec() 是 QApplication 类的成员函数,它启动应用程序的事件循环,开始接收并处理用户输入事件、窗口事件等。这个函数会阻塞,直到应用程序结束。// a.exec() 会返回一个整数值,通常用于表示程序的退出状态。return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <cmath> // 引入cmath以便使用更准确的 M_PIconst static double PI = M_PI; // 使用 M_PI 常量来获得更精确的圆周率// const static double PI = 3.1416;MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_countBtn_clicked()
{bool ok;// QString tempStr;// 获取 QLineEdit 中输入的值QString valueStr = ui->lineEdit->text();// int valueInt = valueStr.toInt(&ok);// 转换输入值为整数,检查转换是否成功int radius = valueStr.toInt(&ok);if (!ok || radius <= 0) { // 输入无效或半径为负数/零ui->areaLabel_2->setText("请输入有效的半径!");return;}// 计算圆形面积double area = radius * radius * PI;// 设置计算结果// ui 是指向 Ui::MainWindow 类的指针,Ui::MainWindow 类包含了界面上的所有控件。// areaLabel_2 是在界面上使用 Qt Designer 创建的标签控件(QLabel)。这个标签通常用于显示计算结果,在这个上下文中,它用于显示圆的面积。// QString::number() 是一个静态成员函数,它将一个数字(如整数、浮点数等)转换为 QString 类型的字符串。它有多个重载版本,可以指定格式和精度ui->areaLabel_2->setText(QString::number(area, 'f', 2)); // 保留两位小数// double area = valueInt * valueInt * PI;// ui->areaLabel_2->setText(tempStr.setNum(area));
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_countBtn_clicked();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><widget class="QWidget" name="centralwidget"><widget class="QLabel" name="areaLabel_1"><property name="geometry"><rect><x>340</x><y>120</y><width>40</width><height>12</height></rect></property><property name="text"><string>面积:</string></property><property name="alignment"><set>Qt::AlignmentFlag::AlignCenter</set></property></widget><widget class="QLabel" name="radiusLabel"><property name="geometry"><rect><x>70</x><y>120</y><width>40</width><height>12</height></rect></property><property name="text"><string>半径:</string></property><property name="alignment"><set>Qt::AlignmentFlag::AlignCenter</set></property></widget><widget class="QLabel" name="areaLabel_2"><property name="geometry"><rect><x>419</x><y>120</y><width>111</width><height>20</height></rect></property><property name="frameShape"><enum>QFrame::Shape::Panel</enum></property><property name="frameShadow"><enum>QFrame::Shadow::Sunken</enum></property><property name="text"><string/></property></widget><widget class="QLineEdit" name="lineEdit"><property name="geometry"><rect><x>140</x><y>120</y><width>141</width><height>20</height></rect></property></widget><widget class="QPushButton" name="countBtn"><property name="geometry"><rect><x>300</x><y>220</y><width>80</width><height>18</height></rect></property><property name="text"><string>计算</string></property></widget></widget><widget class="QMenuBar" name="menubar"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>17</height></rect></property></widget><widget class="QStatusBar" name="statusbar"/></widget><resources/><connections/>
</ui>
项目树形框架

运行结果


用qmake构建系统创建新项目


项目树形框架

main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"#include <QGridLayout>
#include <cmath> // 引入cmath以便使用更准确的 M_PI// M_PI 是 cmath 库中的一个常量,但它并不在所有平台上都可用
// const static double PI = M_PI; // 使用 M_PI 常量来获得更精确的圆周率const double PI = 3.14159;void MainWindow::showArea() {bool ok;QString valueStr = lineEdit->text();int radius = valueStr.toInt(&ok);if(!ok || radius <= 0) {label2->setText("请输入有效的半径!");return;}double area = radius * radius * PI;label2->setText(QString::number(area, 'f', 2));
}// MainWindow::MainWindow(QWidget *parent):这是 MainWindow 类的构造函数,接受一个 QWidget 类型的父窗口(parent)指针。
// QMainWindow 是 Qt 提供的一个主窗口类,MainWindow 继承自 QMainWindow,因此会使用 QMainWindow 的构造函数
// QMainWindow(parent):调用基类(QMainWindow)的构造函数,parent 参数用来指定父窗口,通常是 nullptr,表示没有父窗口。
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{// label1 = new QLabel(this):创建一个 QLabel 对象 label1,用于显示文本。this 指定 label1 的父控件是当前窗口(MainWindow)label1 = new QLabel(this);// 设置 label1 的文本为“请输入圆的半径:”。tr() 是 Qt 提供的一个翻译函数,通常用于国际化支持,使文本可以根据不同语言进行翻译label1->setText(tr("请输入圆的半径:"));// 创建一个 QLineEdit 控件,它是一个文本输入框,用户可以在其中输入圆的半径。lineEdit = new QLineEdit(this);// 创建另一个 QLabel 控件,暂时没有设置它的文本内容,这个控件后续可用来显示计算出的圆的面积label2 = new QLabel(this);// 创建一个 QPushButton 按钮,this 表示按钮的父控件是 MainWindowbutton = new QPushButton(this);button->setText("计算对应圆的面积");// 创建一个 QGridLayout 布局管理器。QGridLayout 会将控件按照网格(行列)排列。这里传入 this 是将布局应用于当前窗口(MainWindow)的窗口QGridLayout *mainLayout = new QGridLayout(this);mainLayout->addWidget(label1, 0, 0);mainLayout->addWidget(lineEdit, 0, 1);mainLayout->addWidget(label2, 1, 0);mainLayout->addWidget(button, 1, 1);// 创建一个中心部件并设置布局// QMainWindow 是一个复杂的窗口部件,它通常包含菜单栏、工具栏、状态栏和中心部件。中心部件是主要的内容显示区域,应该设置布局QWidget *centralWidget = new QWidget(this); // 创建中心部件centralWidget->setLayout(mainLayout); // 设置布局setCentralWidget(centralWidget); // 设置中心部件// 连接按钮点击事件,使用 Qt6 的新语法connect(button, &QPushButton::clicked, this, &MainWindow::showArea);// connect(button, SIGNAL(clicked()), this, SLOT(showArea()));// 等同于 connect(button, "clicked()", this, "showArea()");// 将创建好的布局 mainLayout 设置为 MainWindow 的布局管理器。这样,mainLayout 中的控件就会在窗口中按照设置的网格布局显示。// setLayout(mainLayout);// QWidget::setLayout(mainLayout);
}MainWindow::~MainWindow() {}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QLabel>
#include <QLineEdit>
#include <QPushButton>#include <QMainWindow>class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:QLabel *label1, *label2;QLineEdit *lineEdit;QPushButton *button;private slots:void showArea();
};
#endif // MAINWINDOW_H
运行结果


之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!
