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

news2024/10/6 2:26:21

前提条件:

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_NAMESPACE

class 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);
    } // setupUi

    void 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 Ui

QT_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_OBJECT

public:
    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_OBJECT

public:
    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.coloradmin.cn/o/1034155.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Matlab信号处理:FFT频谱分辨率

频谱分辨率&#xff1a; 其中为采样间隔&#xff0c;为采样点数。 FFT分辨率&#xff1a; 其中为采样频率&#xff0c;为FFT点数。 有两正弦函数&#xff0c;频率分别为 f1 1Hz&#xff0c;f2 10Hz&#xff0c;f3 40Hz&#xff1b; 示例1&#xff1a; 采样频率 fs 1000H…

LwIP笔记02:

一、LwIP源文件 api&#xff1a;NETCONN API 和 Socket API 相关的源文件&#xff0c;在有操作系统环境下使用 apps&#xff1a;应用程序源文件&#xff0c;如http、mqtt、tftp等 core&#xff1a;LwIP内核源文件 include&#xff1a;LwIP所有模块对应的头文件 netif&…

基于AlgoT1设备改进多源融合定位算法(GNSS+INS+VISION)

AlgoT1是融合了GNSSIMUVISION的数据平台&#xff0c;用该设备实测了一组数据&#xff0c;并且在开源代码上进行了改进&#xff0c;得到的效果还行&#xff0c;对做多源融合算法研究是个不错的选择。 0.设备图 这款设备是上海代数律动技术有限公司新出的机器(http://www.algot…

2023 年 Android 毕业设计选题推荐,200 道 Android 毕业设计题目,避免踩坑

前言 选择一个Android毕业设计题目是一个重要的决策&#xff0c;它将影响你未来几个月的工作。以下是一些关于如何选择一个合适的Android毕业设计题目以及如何避免踩坑的建议&#xff1a; 兴趣和热情&#xff1a;首先&#xff0c;选择你真正感兴趣的领域。如果你对某个领域充…

【算法思想】排序

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kuan 的首页,持续学…

Spring面试题12:Spring中IOC的优缺点是什么?IOC依赖注入方式有哪些

该文章专注于面试,面试只要回答关键点即可,不需要对框架有非常深入的回答,如果你想应付面试,是足够了,抓住关键点 面试官:Spring中IOC的优缺点是什么? IOC(Inversion of Control,控制反转)是Spring框架的一个重要特性,它实现了对象的创建和依赖关系的管理的反转。…

Activiti7工作流 一【工作流介绍、什么是Activiti7?、Activiti7环境、集成Activiti7、流程引擎API】

文章目录 Activiti7工作流一、工作流介绍1.1 概念1.2 适用行业1.3 应用领域1.4 传统实现方式1.5 什么是工作流引擎 二、什么是Activiti7&#xff1f;2.1 概述2.2 Activiti7内部核心机制2.3 BPMN2.4 Activiti如何使用2.4.1 整合Activiti2.4.2 业务流程建模2.4.3 部署业务流程2.4…

小说界的卷王巴尔扎克,咖啡续命拼命搞钱

巴尔扎克每天工作18小时&#xff0c;咖啡续命&#xff0c;活活累死。 巴尔扎克高产似母猪&#xff0c;写了90多部小说。 巴尔扎克很肤浅&#xff0c;除了写小说&#xff0c;就是搞钱&#xff0c;却一直是贫穷的状态。 一、卷王 1799年&#xff0c;奥诺雷德巴尔扎克出生在法国…

怎么快速提取图片中的文字信息?怎么使用OCR图片文字提取一键提取文字

图片里的文字如何提取?一些图片中的文字信息是我们需要的&#xff0c;但是一个个输入太麻烦了&#xff0c;怎么将图片上的文字提取出来?Initiator是一款易于使用的小型 macOS OCR&#xff08;光学字符识别&#xff09;应用程序&#xff0c;可提取和识别 Mac 计算机屏幕上的任…

自监督学习之对比学习:MoCo模型超级详解解读+总结

文章目录 一、MoCo简介1.1 整体思想1.2 动量1.3 正负样本如何选取 二、动态字典2.1 query和key2.2 字典特点 三、编码器的动量更新3.1 编码器的更新规则3.2 使用动量更新的原因 四、实验过程4.1 目标函数&#xff1a;infoNCE4.1.1 softmax4.1.2 交叉熵损失4.1.3 交叉熵损失函数…

【蓝桥杯选拔赛真题62】Scratch判断小球 少儿编程scratch图形化编程 蓝桥杯选拔赛真题解析

目录 scratch判断小球 一、题目要求 编程实现 二、案例分析 1、角色分析

BottomNavigationView3个以上图标不显示文字

问题 当BottomNavigationView设置的菜单中超过三个图标时&#xff0c;出现只有焦点聚集到图标时才会显示底部设置的文字描述&#xff0c;当没有焦点聚集则只显示图标&#xff0c;效果如下&#xff1a; 解决办法 设置labelVisibilityMode值 如果BottomNavigationItemView类并…

Jmeter——结合Allure展示测试报告

在平时用jmeter做测试时&#xff0c;生成报告的模板&#xff0c;不是特别好。大家应该也知道allure报告&#xff0c;页面美观。 先来看效果图&#xff0c;报告首页&#xff0c;如下所示&#xff1a; 报告详情信息&#xff0c;如下所示&#xff1a; 运行run.py文件&#xff0c;…

Java笔记:认识一下class文件

1.class文件概述 我们可任意打开一个Class文件&#xff08;使用Hex Editor等工具打开&#xff09;&#xff0c;内容如下&#xff08;内容是16进制&#xff09;&#xff1a; 十六进制转字符串&#xff1a;http://www.bejson.com/convert/ox2str/ 进制转换网址&#xff08;十六进…

谷歌浏览器jsonView插件安装与使用

1、打开 https://github.com &#xff1b; 2、搜索 jsonView 链接&#xff1a;https://gitee.com/wangl2020/chrome_JSONVue 3、选择需要的插件我是选这个&#xff1b; 4、点击【Download Zip】&#xff0c;插件下载完成&#xff0c;解压缩到相应目录&#xff08;D:\Downloa…

外贸电商独立站的选品和运营

第一步&#xff1a;选品 做出口跨境电商卖家&#xff0c;最难回答的问题就是我要卖什么产品&#xff1f;销量好的产品&#xff0c;竞争太激烈&#xff1b;价格很高的又卖不动&#xff1b;太小众的又担心客户不好开发&#xff0c;很纠结&#xff01; 的确&#xff0c;对于出口B2…

若依cloud -【 100 ~ 103 】

100 分布式日志介绍 | RuoYi 分布式日志就相当于把日志存储在不同的设备上面。比如若依项目中有ruoyi-modules-file、ruoyi-modules-gen、ruoyi-modules-job、ruoyi-modules-system四个应用&#xff0c;每个应用都部署在单独的一台机器里边&#xff0c;应用对应的日志的也单独存…

数据结构-----堆(完全二叉树)

目录 前言 一.堆 1.堆的概念 2.堆的存储方式 二.堆的操作方法 1.堆的结构体表示 2.数字交换接口函数 3.向上调整&#xff08;难点&#xff09; 4.向下调整&#xff08;难点&#xff09; 5.创建堆 6.堆的插入 7.判断空 8.堆的删除 9.获取堆的根(顶)元素 10.堆的遍历…

Linux中sudo命令的添加和操作

使用 sudo分配权限 &#xff08;1&#xff09;修改/etc/sudoers 文件分配文件 # chmod 740 /etc/sudoers # vi /etc/sudoers 找到这行&#xff1a;root ALL(ALL) ALL, 在这行下面添加 xxx ALL(ALL) ALL (这里的xxx就是你的普通用户&#xff0c;而ruice就是我的普通用户 ) 编…

nginx部署多个项目

前言 实现在一台服务器上使用nginx部署多个项目的方法 查看并修改nginx安装的默认配置文件 在 Linux 操作系统中&#xff0c;Nginx 在编译安装时默认的配置文件路径是 /usr/local/nginx/conf/nginx.conf。 如果是通过发行版的包管理器安装&#xff0c;则默认的配置文件路径可能…