• Windows 基于Visual Studio 开发Qt 6 注意事项


    前提条件:

    1、Visual  Studio 2022 社区版(免费版)

    2、Qt-6.5.1版本

    Qt Vistual Studio Tools下载

    先打开Visual Studio 2022 社区版 :

    点击扩展-》管理拓展按钮后,在搜索框中输入Qt,点击这里第一个扩展安装。

     Qt Visual Studio Tools 拓展插件安装成功后,在拓展菜单项多出一个菜单选项"Qt VS Tools"

    Vistual Studio 2022 社区版配置Qt 6

    进入Vistual Studio 2022 点击扩展->Qt VS Tool->Qt Versions

    进入Qt Versions后, 点击Qt -> Versions。

    配置Qt 6 地址:D:\Qt\6.5.1\msvc2019_64\bin\

     Vistual Studio 2022 创建Qt 6 项目

    创建一个Qt项目。

    点击下一步:

     

     

    点击下一步:

     

    Qt 应用程序入口: main.cpp

    Qt 应用UI设计文件:QtWidgetsApplication_One.ui

     Qt 应用组件对应头文件:QtWidgetsApplication_One.h

    Qt 应用组件对应Class 文件:QtWidgetsApplication_One.cpp

    Qt 应用继承基础类:QMainWindows

    Qt 应用资源文件:QtWidgetsApplication_One.qrc

    点击Finsh 按钮:

    运行效果展示:

    Vistual Studio 2022 无法正常打开Qt  项目 中的*.UI文件 

    当我们双击*.UI文件,会出现下面这样的弹窗。 

    如何解决此类问题?

    请按照如下步骤进行:

     

     

    右键.ui文件 ===> 打开方式 ===> 添加  ===> 找到对应文件下的.exe文件 ===> 随便取名 

    UI 效果展示:

    Vistual Studio 2022 社区版 Qt 项目 涉及简单登入页面

    在QtWidgetsApplication_One.ui 文件中添加如下组件清单:

    3 个Label 标签

    2 个Line Edit 输入文本框

    2 个Push Button 按钮

    1 个CheckBox Button 按钮

    QtWidgetsApplication_One.ui 在设计器中的展示:

    点击Ctral + S 保存快捷键,再次运行QtWidgetsApplication_One 项目的main.cpp 方法。

     

    Vistual Studio 2022 社区版 Qt 项目 涉及多个UI 页面

    在实际Qt 项目开发中,需要使用多个UI 页面以及对应的头文件和Class 文件。

    Qt 项目-》Form Files 右击

     

    我在这里新增addFrom.ui 文件,且原型设计如下图所示:

     如何解决基于*.UI 文件生成头文件和Class文件?

    请按照如下步骤进行:

    第一步:在Qt 6 项目中选择需要进行编译的UI 文件并点击编译,我这里选择的是自定义addFrom.ui 文件。

    编译的结果:将在Qt 项目 所占目录中 ->X64-》Debug ->uic 文件夹目录中生成一个名为:ui_addFrom.h 的头文件。

    查看文件内容(ui_addFrom.h)

    1. /********************************************************************************
    2. ** Form generated from reading UI file 'addFrom.ui'
    3. **
    4. ** Created by: Qt User Interface Compiler version 6.5.1
    5. **
    6. ** WARNING! All changes made in this file will be lost when recompiling UI file!
    7. ********************************************************************************/
    8. #ifndef UI_ADDFROM_H
    9. #define UI_ADDFROM_H
    10. #include <QtCore/QVariant>
    11. #include <QtWidgets/QApplication>
    12. #include <QtWidgets/QLabel>
    13. #include <QtWidgets/QLineEdit>
    14. #include <QtWidgets/QPushButton>
    15. #include <QtWidgets/QRadioButton>
    16. #include <QtWidgets/QTextEdit>
    17. #include <QtWidgets/QWidget>
    18. QT_BEGIN_NAMESPACE
    19. class Ui_Form
    20. {
    21. public:
    22. QLabel *label;
    23. QLabel *label_2;
    24. QLineEdit *lineEdit;
    25. QLabel *label_3;
    26. QLineEdit *lineEdit_2;
    27. QLabel *label_4;
    28. QLineEdit *lineEdit_3;
    29. QLabel *label_5;
    30. QRadioButton *radioButton;
    31. QRadioButton *radioButton_2;
    32. QLabel *label_6;
    33. QTextEdit *textEdit;
    34. QPushButton *pushButton;
    35. void setupUi(QWidget *Form)
    36. {
    37. if (Form->objectName().isEmpty())
    38. Form->setObjectName("Form");
    39. Form->resize(650, 455);
    40. label = new QLabel(Form);
    41. label->setObjectName("label");
    42. label->setGeometry(QRect(210, 50, 54, 16));
    43. label_2 = new QLabel(Form);
    44. label_2->setObjectName("label_2");
    45. label_2->setGeometry(QRect(140, 90, 54, 16));
    46. lineEdit = new QLineEdit(Form);
    47. lineEdit->setObjectName("lineEdit");
    48. lineEdit->setGeometry(QRect(210, 90, 113, 20));
    49. label_3 = new QLabel(Form);
    50. label_3->setObjectName("label_3");
    51. label_3->setGeometry(QRect(140, 130, 54, 16));
    52. lineEdit_2 = new QLineEdit(Form);
    53. lineEdit_2->setObjectName("lineEdit_2");
    54. lineEdit_2->setGeometry(QRect(210, 130, 113, 20));
    55. label_4 = new QLabel(Form);
    56. label_4->setObjectName("label_4");
    57. label_4->setGeometry(QRect(140, 170, 54, 16));
    58. lineEdit_3 = new QLineEdit(Form);
    59. lineEdit_3->setObjectName("lineEdit_3");
    60. lineEdit_3->setGeometry(QRect(210, 170, 113, 20));
    61. label_5 = new QLabel(Form);
    62. label_5->setObjectName("label_5");
    63. label_5->setGeometry(QRect(140, 210, 54, 16));
    64. radioButton = new QRadioButton(Form);
    65. radioButton->setObjectName("radioButton");
    66. radioButton->setGeometry(QRect(210, 210, 95, 20));
    67. radioButton_2 = new QRadioButton(Form);
    68. radioButton_2->setObjectName("radioButton_2");
    69. radioButton_2->setGeometry(QRect(290, 210, 95, 20));
    70. label_6 = new QLabel(Form);
    71. label_6->setObjectName("label_6");
    72. label_6->setGeometry(QRect(140, 250, 54, 16));
    73. textEdit = new QTextEdit(Form);
    74. textEdit->setObjectName("textEdit");
    75. textEdit->setGeometry(QRect(140, 270, 191, 81));
    76. pushButton = new QPushButton(Form);
    77. pushButton->setObjectName("pushButton");
    78. pushButton->setGeometry(QRect(210, 380, 75, 24));
    79. retranslateUi(Form);
    80. QMetaObject::connectSlotsByName(Form);
    81. } // setupUi
    82. void retranslateUi(QWidget *Form)
    83. {
    84. Form->setWindowTitle(QCoreApplication::translate("Form", "Form", nullptr));
    85. label->setText(QCoreApplication::translate("Form", "\347\224\250\346\210\267\346\226\260\345\242\236", nullptr));
    86. label_2->setText(QCoreApplication::translate("Form", "\345\247\223\345\220\215", nullptr));
    87. label_3->setText(QCoreApplication::translate("Form", "\345\271\264\351\276\204", nullptr));
    88. label_4->setText(QCoreApplication::translate("Form", "\345\256\266\345\272\255\344\275\217\345\235\200", nullptr));
    89. label_5->setText(QCoreApplication::translate("Form", "\346\200\247\345\210\253:", nullptr));
    90. radioButton->setText(QCoreApplication::translate("Form", "\347\224\267", nullptr));
    91. radioButton_2->setText(QCoreApplication::translate("Form", "\345\245\263", nullptr));
    92. label_6->setText(QCoreApplication::translate("Form", "\344\270\252\344\272\272\347\256\200\344\273\213\357\274\232", nullptr));
    93. pushButton->setText(QCoreApplication::translate("Form", "\344\277\235\345\255\230", nullptr));
    94. } // retranslateUi
    95. };
    96. namespace Ui {
    97. class Form: public Ui_Form {};
    98. } // namespace Ui
    99. QT_END_NAMESPACE
    100. #endif // UI_ADDFROM_H

    重点: 

    1、声明一个命名空间:namespae Ui。

    2、定义自定义addFrom.ui 对应的实现类:Ui_From。

    3、定义类Ui_From 在命名空间Ui 访问别名 From。 

    第二步:在Qt 项目->Header Files 添加addFrom.ui 对应的AddFrom.h 头文件。

    主要用途:引用"ui_addFrom.h" 头文件中对addFrom.ui 实现类:Ui::Form ui

    addFrom.h 头文件 源码:

    1. #pragma once
    2. #include
    3. #include "ui_addFrom.h"
    4. class AddFrom : public QMainWindow
    5. {
    6. Q_OBJECT
    7. public:
    8. AddFrom(QWidget* parent = nullptr);
    9. ~AddFrom();
    10. private:
    11. Ui::Form ui;
    12. };

     第三步:在Qt 项目->Source Files 添加addFrom.h 对应的实现类addFrom.cpp。

    1. #include "addFrom.h"
    2. AddFrom::AddFrom(QWidget* parent)
    3. : QMainWindow(parent)
    4. {
    5. ui.setupUi(this);
    6. }
    7. AddFrom::~AddFrom()
    8. {}

    项目整体结构:

    第四步:在程序入库main.cpp 文件替换为addFrom.h 文件

    1. #include "QtWidgetsApplication_One.h"
    2. #include "addFrom.h"
    3. #include
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. AddFrom w;
    8. w.show();
    9. return a.exec();
    10. }

    效果截图 :

    Vistual Studio 2022 社区版 Qt 项目 涉及多个UI 页面跳转

    在上一个案列中,我们涉及了登入页面和注册页面,如何把两个页面串联, 我们将通过槽和函数实现 ,通过登入页面的登入按钮跳转至注册页面。

    第一步:在QtWidgetsApplication_One.h 头文件中定义槽函数:

     // 定义槽函数
    public slots:
        void gotoFrom();

    QtWidgetsApplication_One.h  源文件:

    1. #pragma once
    2. #include
    3. #include "ui_QtWidgetsApplication_One.h"
    4. class QtWidgetsApplication_One : public QMainWindow
    5. {
    6. Q_OBJECT
    7. public:
    8. QtWidgetsApplication_One(QWidget *parent = nullptr);
    9. ~QtWidgetsApplication_One();
    10. // 定义槽函数
    11. public slots:
    12. void gotoFrom();
    13. private:
    14. Ui::QtWidgetsApplication_OneClass ui;
    15. };

     第二步:在QtWidgetsApplication_One.h 头文件对应QtWidgetsApplication_One.cpp 文件,实现PushButton 信号事件与槽函数绑定。

    1. 核心代码片段:
    2. 在初始化构造函数中完成PushButton 信号事件与自定义槽函数绑定。
    3. QtWidgetsApplication_One::QtWidgetsApplication_One(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. ui.setupUi(this);
    7. //连接pushbutton的clicked()信号到自定义的gotoFrom()槽
    8. connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(gotoFrom()));
    9. }
    10. 实现在QtWidgetsApplication_One.h 自定义槽函数gotoFrom()
    11. void QtWidgetsApplication_One::gotoFrom() {
    12. qDebug() << "pushButton 按钮点击事件触发 ";
    13. }
    14. // 自定义槽函数gotoFrom():仅仅涉及控制台字符串输出,暂时不涉及页面跳转。

    QtWidgetsApplication_One.cpp 控制台输出字符串源码。

    1. #include "QtWidgetsApplication_One.h"
    2. #include
    3. QtWidgetsApplication_One::QtWidgetsApplication_One(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. ui.setupUi(this);
    7. //连接pushbutton的clicked()信号到自定义的gotoFrom()槽
    8. connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(gotoFrom()));
    9. }
    10. QtWidgetsApplication_One::~QtWidgetsApplication_One()
    11. {}
    12. void QtWidgetsApplication_One::gotoFrom() {
    13. qDebug() << "pushButton 按钮点击事件触发 ";
    14. }

    效果展示:

     美中不足:输出的汉子显示乱码。

     第三步:在QtWidgetsApplication_One.cpp 文件中,添加对跳转页面头文件的依赖("addFrom.h")。并在槽函数gotoFrom()中实现页面跳转。

    1. 核心代码片段:
    2. 1、添加跳转页面的头文件
    3. #include "addFrom.h"
    4. 2、在自定义槽函数实现页面跳转
    5. void QtWidgetsApplication_One::gotoFrom() {
    6. qDebug() << "pushButton 按钮点击事件触发 ";
    7. // 当前页面关闭
    8. this->close();
    9. // 跳转页面显示
    10. AddFrom* from = new AddFrom();
    11. from->show();
    12. }

    QtWidgetsApplication_One.cpp 页面跳转。

    1. #include "QtWidgetsApplication_One.h"
    2. #include "addFrom.h"
    3. #include <QDebug>
    4. QtWidgetsApplication_One::QtWidgetsApplication_One(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. ui.setupUi(this);
    8. //连接pushbutton的clicked()信号到自定义的gotoFrom()槽
    9. connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(gotoFrom()));
    10. }
    11. QtWidgetsApplication_One::~QtWidgetsApplication_One()
    12. {}
    13. void QtWidgetsApplication_One::gotoFrom() {
    14. qDebug() << "pushButton 按钮点击事件触发 ";
    15. // 当前页面关闭
    16. this->close();
    17. // 跳转页面显示
    18. AddFrom* from = new AddFrom();
    19. from->show();
    20. }

    效果展示:

     

    至此,Visual Studio 2022 + Qt 6 在Windows 开发环境注意细节就 讲这么多了。 

  • 相关阅读:
    在CDH的hue上的oozie出现,提交 Coordinator My Schedule 时出错
    从虚拟机上安装linux以及linux的一些简单教程(一)
    MySQL数据加密,模糊查询
    【C】单链表
    支付宝小程序 头部导航背景颜色渐变
    718. 最长重复子数组
    计算机毕业设计django基于python大学生多媒体学习系统(源码+系统+mysql数据库+Lw文档)
    Java应用生产Full GC或者OOM问题如何定位
    java毕业设计病人追踪治疗信息系统mybatis+源码+调试部署+系统+数据库+lw
    关于ASCII码的了解
  • 原文地址:https://blog.csdn.net/zhouzhiwengang/article/details/133050078