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)

/********************************************************************************
** Form generated from reading UI file 'addFrom.ui'
**
** Created by: Qt User Interface Compiler version 6.5.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/#ifndef UI_ADDFROM_H
#define UI_ADDFROM_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_Form
{
public:QLabel *label;QLabel *label_2;QLineEdit *lineEdit;QLabel *label_3;QLineEdit *lineEdit_2;QLabel *label_4;QLineEdit *lineEdit_3;QLabel *label_5;QRadioButton *radioButton;QRadioButton *radioButton_2;QLabel *label_6;QTextEdit *textEdit;QPushButton *pushButton;void setupUi(QWidget *Form){if (Form->objectName().isEmpty())Form->setObjectName("Form");Form->resize(650, 455);label = new QLabel(Form);label->setObjectName("label");label->setGeometry(QRect(210, 50, 54, 16));label_2 = new QLabel(Form);label_2->setObjectName("label_2");label_2->setGeometry(QRect(140, 90, 54, 16));lineEdit = new QLineEdit(Form);lineEdit->setObjectName("lineEdit");lineEdit->setGeometry(QRect(210, 90, 113, 20));label_3 = new QLabel(Form);label_3->setObjectName("label_3");label_3->setGeometry(QRect(140, 130, 54, 16));lineEdit_2 = new QLineEdit(Form);lineEdit_2->setObjectName("lineEdit_2");lineEdit_2->setGeometry(QRect(210, 130, 113, 20));label_4 = new QLabel(Form);label_4->setObjectName("label_4");label_4->setGeometry(QRect(140, 170, 54, 16));lineEdit_3 = new QLineEdit(Form);lineEdit_3->setObjectName("lineEdit_3");lineEdit_3->setGeometry(QRect(210, 170, 113, 20));label_5 = new QLabel(Form);label_5->setObjectName("label_5");label_5->setGeometry(QRect(140, 210, 54, 16));radioButton = new QRadioButton(Form);radioButton->setObjectName("radioButton");radioButton->setGeometry(QRect(210, 210, 95, 20));radioButton_2 = new QRadioButton(Form);radioButton_2->setObjectName("radioButton_2");radioButton_2->setGeometry(QRect(290, 210, 95, 20));label_6 = new QLabel(Form);label_6->setObjectName("label_6");label_6->setGeometry(QRect(140, 250, 54, 16));textEdit = new QTextEdit(Form);textEdit->setObjectName("textEdit");textEdit->setGeometry(QRect(140, 270, 191, 81));pushButton = new QPushButton(Form);pushButton->setObjectName("pushButton");pushButton->setGeometry(QRect(210, 380, 75, 24));retranslateUi(Form);QMetaObject::connectSlotsByName(Form);} // setupUivoid retranslateUi(QWidget *Form){Form->setWindowTitle(QCoreApplication::translate("Form", "Form", nullptr));label->setText(QCoreApplication::translate("Form", "\347\224\250\346\210\267\346\226\260\345\242\236", nullptr));label_2->setText(QCoreApplication::translate("Form", "\345\247\223\345\220\215", nullptr));label_3->setText(QCoreApplication::translate("Form", "\345\271\264\351\276\204", nullptr));label_4->setText(QCoreApplication::translate("Form", "\345\256\266\345\272\255\344\275\217\345\235\200", nullptr));label_5->setText(QCoreApplication::translate("Form", "\346\200\247\345\210\253:", nullptr));radioButton->setText(QCoreApplication::translate("Form", "\347\224\267", nullptr));radioButton_2->setText(QCoreApplication::translate("Form", "\345\245\263", nullptr));label_6->setText(QCoreApplication::translate("Form", "\344\270\252\344\272\272\347\256\200\344\273\213\357\274\232", nullptr));pushButton->setText(QCoreApplication::translate("Form", "\344\277\235\345\255\230", nullptr));} // retranslateUi};namespace Ui {class Form: public Ui_Form {};
} // namespace UiQT_END_NAMESPACE#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 头文件 源码:

#pragma once#include <QtWidgets/QMainWindow>
#include "ui_addFrom.h"class AddFrom : public QMainWindow
{Q_OBJECTpublic:AddFrom(QWidget* parent = nullptr);~AddFrom();private:Ui::Form ui;
};

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

#include "addFrom.h"AddFrom::AddFrom(QWidget* parent): QMainWindow(parent)
{ui.setupUi(this);
}AddFrom::~AddFrom()
{}

项目整体结构:

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

#include "QtWidgetsApplication_One.h"
#include "addFrom.h"
#include <QtWidgets/QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);AddFrom w;w.show();return a.exec();
}

效果截图 :

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

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

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

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

QtWidgetsApplication_One.h  源文件:

#pragma once#include <QtWidgets/QMainWindow>
#include "ui_QtWidgetsApplication_One.h"class QtWidgetsApplication_One : public QMainWindow
{Q_OBJECTpublic:QtWidgetsApplication_One(QWidget *parent = nullptr);~QtWidgetsApplication_One();// 定义槽函数
public slots:void gotoFrom();private:Ui::QtWidgetsApplication_OneClass ui;
};

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

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

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

#include "QtWidgetsApplication_One.h"
#include <QDebug>QtWidgetsApplication_One::QtWidgetsApplication_One(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);//连接pushbutton的clicked()信号到自定义的gotoFrom()槽connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(gotoFrom()));
}QtWidgetsApplication_One::~QtWidgetsApplication_One()
{}void QtWidgetsApplication_One::gotoFrom() {qDebug() << "pushButton 按钮点击事件触发 ";
}

效果展示:

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

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

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

QtWidgetsApplication_One.cpp 页面跳转。

#include "QtWidgetsApplication_One.h"
#include "addFrom.h"
#include <QDebug>QtWidgetsApplication_One::QtWidgetsApplication_One(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);//连接pushbutton的clicked()信号到自定义的gotoFrom()槽connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(gotoFrom()));
}QtWidgetsApplication_One::~QtWidgetsApplication_One()
{}void QtWidgetsApplication_One::gotoFrom() {qDebug() << "pushButton 按钮点击事件触发 ";// 当前页面关闭this->close();// 跳转页面显示AddFrom* from = new AddFrom();from->show();
}

效果展示:

 

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/115938.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python方法汇总:轻松实现功能!

在爬虫开发中&#xff0c;有时需要模拟登录网站以获取更多的数据或执行特定的操作。本文将为你总结几种常用的Python爬虫模拟登录方法&#xff0c;帮助你轻松实现登录功能&#xff0c;让你的爬虫更加强大有用。 一、基于Requests库的模拟登录 1. 使用Session对象&#xff1a;…

MediaPipe+OpenCV 实现实时手势识别(附Python源码)

MediaPipe官网&#xff1a;https://developers.google.com/mediapipe MediaPipe仓库&#xff1a;https://github.com/google/mediapipe 一、MediaPipe介绍 MediaPipe 是一个由 Google 开发的开源跨平台机器学习框架&#xff0c;用于构建视觉和感知应用程序。它提供了一系列预训…

实例讲解Spring boot动态切换数据源

前言 在公司的系统里&#xff0c;由于数据量较大&#xff0c;所以配置了多个数据源&#xff0c;它会根据用户所在的地区去查询那一个数据库&#xff0c;这样就产生了动态切换数据源的场景。 今天&#xff0c;就模拟一下在主库查询订单信息查询不到的时候&#xff0c;切换数据…

GeoServer地图服务器权限控制

目录 1下载相关软件 2部署软件 3配置鉴权环节 4Java工程 5测试鉴权 6测试鉴权结果分析 本文章应该会后面试验一个鉴权功能就会发布一系列测试过程&#xff08;GeoServer有很多鉴权方式&#xff09; 1Download - GeoServer 1下载相关软件 进入geoserver官网的下载页面 …

Ingress Controller

什么是 Ingress Controller &#xff1f; 在云原生生态中&#xff0c;通常来讲&#xff0c;入口控制器( Ingress Controller )是 Kubernetes 中的一个关键组件&#xff0c;用于管理入口资源对象。 Ingress 资源对象用于定义来自外网的 HTTP 和 HTTPS 规则&#xff0c;以控制进…

创建一个简单的外卖订餐系统

在今天的快节奏生活中&#xff0c;外卖订餐系统已经成为了人们日常生活中不可或缺的一部分。这些系统通过在线点餐和配送服务&#xff0c;为用户提供了便捷的用餐体验。在本文中&#xff0c;我们将创建一个简单的外卖订餐系统&#xff0c;使用Python和Flask框架构建后端&#x…

数据库中的DDL与DML

标签&#xff1a;数据库 记录下DDL和DML的相关概念。 数据定义语言 定义数据库模式 数据定义语言DDL(Data-Definition Language)可定义数据库模式。数据库模式在之前的文章中已经提到过了。简单来说&#xff0c;建表用的SQL语句就是DDL。如下代码 CREATE TABLE department(de…

【进阶C语言】字符串与内存库函数认识与模拟实现

本章内容大致目录&#xff1a; 1.strlen函数 2.strcpy函数 3.strcmp函数 4.strcat函数 5.strstr函数 6.strtok函数 7.strerror与perror函数 8.字符操作函数 9.内存操作函数 10.总结 以上函数均属于库函数&#xff0c;有的函数则会介绍如何模拟实现。 一、strlen函数…

蓝桥杯每日一题2023.9.22

4960. 子串简写 - AcWing题库 题目描述 题目分析 原本为纯暴力但是发现会超时&#xff0c;可以加入前缀和&#xff0c;从前往后先记录一下每个位置c1出现的次数 再从前往后扫一遍&#xff0c;如果遇到c2就将答案加上此位置前的所有c1的个数&#xff08;直接加上此位置的前缀…

Ubuntu22.04 vnc远程黑屏

一、原因 原因是Ubuntu22.04使用的gnome启用了Wayland。vnc、teamviewer、向日葵、todesk等均无法使用或者远程黑屏等。 简单的说vnc、teamviewer、向日葵、todesk等均基于xorg实现&#xff08;xorg太流行&#xff09;&#xff0c;并不兼容Wayland&#xff0c;所以vnc无法正常…

iOS 17 Simulator Failed with HTTP status 400:bad request

升级 xcode 15 要 ios17 的 sdk 才能运行&#xff0c;但是更新这个 sdk 400 错误了 解决方案&#xff1a; 直接去官网下载开发者后台下载dmg文件&#xff0c;使用命令行快速安装即可 https://developer.apple.com/documentation/xcode/installing-additional-simulator-runti…

医学影像信息(PACS)系统软件源码

PACS系统是PictureArchivingandCommunicationSystems的缩写&#xff0c;与临床信息系统&#xff08;ClinicalInformationSystem,CIS&#xff09;、放射学信息系统(RadiologyInformationSystem,RIS)、医院信息系统(HospitalInformationSystem,HIS)、实验室信息系统&#xff08;L…