QT C++实现点击按键弹出窗口并显示图片/视频|多窗口应用程序的设计和开发

一、介绍

首先,QT界面开发中主要大体分为2种多窗口的形式:

  • 嵌入式:
    新生成的窗口嵌入在主窗口内部
  • 独立窗口:
    以弹窗形式的新窗口生成和展示
    在这里插入图片描述
    在这里插入图片描述
    这里就讲解最简单的:点击案件后,跳出一个新窗口

二、代码实现

要在Qt C++中实现点击按钮后显示新窗口,并在新窗口中显示由主程序生成的图片,你需要创建两个窗口类:主窗口类和图片显示窗口类。下面是一个简单的示例,展示了如何实现这一功能:
创建一个新的Qt Widgets Application。
添加两个窗口类,一个是主窗口类 MainWindow,另一个是显示图片的窗口类 ImageWindow

一般用vs2019开发,创建Qt Widgets Application之后,会自带一个和项目名同名的主窗口类。这个时候需要手动创建另外一个,即新窗口类。创建方法可以参考这篇博客:Qt5.12.6 + VS2019 点击按钮弹出新窗口

首先是 ImageWindow 类的实现(假设图片使用 QLabel 来显示):
imagewindow.h:

#ifndef IMAGEWINDOW_H
#define IMAGEWINDOW_H

#include <QWidget>
#include <QLabel>

class ImageWindow : public QWidget
{
    Q_OBJECT

public:
    explicit ImageWindow(QWidget *parent = nullptr);
    void displayImage(const QPixmap &pixmap);

private:
    QLabel *imageLabel;

};

#endif // IMAGEWINDOW_H

imagewindow.cpp:

#include "imagewindow.h"
#include <QVBoxLayout>

ImageWindow::ImageWindow(QWidget *parent) : QWidget(parent)
{
    imageLabel = new QLabel;
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(imageLabel);
    setLayout(layout);
}

void ImageWindow::displayImage(const QPixmap &pixmap)
{
    imageLabel->setPixmap(pixmap);
}

然后是主窗口类 MainWindow,当点击按钮时,将创建图片显示窗口的实例,并显示图片:
mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include "imagewindow.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

private slots:
    void on_showImageButton_clicked();

private:
    QPushButton *showImageButton;
    ImageWindow *imageWindow;
};

#endif // MAINWINDOW_H

mainwindow.cpp:

#include "imagewindow.h"

ImageWindow::ImageWindow(QWidget *parent) : QWidget(parent)
{
    imageLabel = new QLabel(this); // 使用 this 作为父对象初始化 QLabel
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(imageLabel);
    setLayout(layout);
}

void ImageWindow::displayImage(const QPixmap &pixmap)
{
    imageLabel->setPixmap(pixmap);
}

三、实战+经验分享

这里我的项目的情况是:

  • 主窗口的cpp文件new_QT_python.cpp:中主要运行项目主体代码,其中开设子线程(视频检测线程,会把事实的检测结果以图片的形式传回主线程)
  • 需求:当按下开始检测的按钮后,立即弹出新窗口,将主线程(主窗口)中接受到检测线程传过来的结果图片在新窗口中进行显示。

Tips:为了突出多窗口的实现,省略了与此无关的代码

new_QT_python.h:

#include <QtWidgets/QMainWindow>
#include "ui_new_QT_python.h"
#include <opencv2/opencv.hpp>
#include "DetectionWindow.h"
//#include "inference.h"

#pragma execution_character_set("utf-8")
#ifdef Q_OS_WIN
#pragma execution_character_set("utf-8") //解决 VS编译器下中文乱码
#endif

class new_QT_python : public QMainWindow
{
    Q_OBJECT

public:
    new_QT_python(QWidget *parent = nullptr);
    ~new_QT_python();  
private:
    Ui::new_QT_pythonClass ui;
    VideoDetectionWorker m_detectionWorker1;
    QThread m_detectionThread1;
    DetectionWindow *imageWindow;
   

private slots:
 
    void onDetectionResult1(const QImage& img);

};

new_QT_python.cpp:

#include "new_QT_python.h"
#include <QTimer.h>
#include <QFileDialog>
#include <iostream>
#include <iomanip>
#include <filesystem>
#include <fstream>
#include <random>
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include "DetectionWindow.h"

#pragma execution_character_set("utf-8")
#ifdef Q_OS_WIN
#pragma execution_character_set("utf-8") //解决 VS编译器下中文乱码
#endif

#define ORT_API_MANUAL_INIT

new_QT_python::new_QT_python(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    
    // 连接子线程的检测结果信号
    connect(&m_detectionWorker1, &VideoDetectionWorker::detectionResult, this, &new_QT_python::onDetectionResult1);

    imageWindow = new DetectionWindow; // Create the image window but don't show it yet
    // 将自定义线程对象移动到子线程中
    m_detectionWorker1.moveToThread(&m_detectionThread1);
}

new_QT_python::~new_QT_python()
{
    m_detectionThread1.quit();
    m_detectionThread1.wait();
}


void new_QT_python::onStartDetectionClicked()
{
    //显示检测过程的窗口
    imageWindow->show();
    // 启动子线程
    m_detectionThread1.start();
    // 检查子线程是否成功启动
    if (m_detectionThread1.isRunning())
    {
        // 子线程已成功启动
        // 在这里添加你的逻辑
        ui.status->setText(" m_detectionThread1 OK");
    }
   
    // 保存视频路径到子线程对象的成员变量
    m_detectionWorker1.m_videoPath = videoPath1;

    // 向子线程发送开始检测的信号
    QMetaObject::invokeMethod(&m_detectionWorker1, "startDetection", Qt::QueuedConnection);

  // 禁用按钮
    ui.startToDetect->setEnabled(false);
}

void new_QT_python::onDetectionResult1(const QImage& img)
{
    imageWindow->displayImage(img);
} 

DetectionWindow.h

#pragma once

#include <QMainWindow>
#include "ui_DetectionWindow.h"

class DetectionWindow : public QMainWindow
{
	Q_OBJECT

public:
	DetectionWindow(QWidget *parent = nullptr);
	void displayImage(const QImage& img);
	~DetectionWindow();

private:
	Ui::DetectionWindowClass ui;
};

DetectionWindow.cpp

#include "DetectionWindow.h"
#include <QVBoxLayout>

DetectionWindow::DetectionWindow(QWidget *parent)
	: QMainWindow(parent)
{	
	ui.setupUi(this);
}
void DetectionWindow::displayImage(const QImage& img)
{
	//使图片能和label尺寸相适应
	ui.imageLabel->setPixmap(QPixmap::fromImage(img).scaled(ui.imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
	ui.imageLabel->setAlignment(Qt::AlignCenter);	
}
DetectionWindow::~DetectionWindow()
{}

四、结果展示

点击“开始检测”按钮,触发void new_QT_python::onStartDetectionClicked()函数,检测子线程启动,像主线程发送图片结果信号,onDetectionResult1函数负责让子窗口对象将图片在其窗口内展示。
在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

%00截断 [GKCTF 2020]cve版签到

打开题目 F12之后在Headers中发现hint 两者结合利用零字符截断使get_headers()请求到本地127.0.0. 结合链接 构造 ?urlhttp://127.0.0.1%00www.ctfhub.com 必须以123结尾 ?urlhttp://127.0.0.123%00www.ctfhub.com 得到flag 知识点&#xff1a; PHP中get_headers函数 g…

python爬虫实战:获取电子邮件和联系人信息

引言 在数字时代&#xff0c;电子邮件和联系人信息成为了许多企业和个人重要的资源&#xff0c;在本文中&#xff0c;我们将探讨如何使用Python爬虫从网页中提取电子邮件和联系人信息&#xff0c;并附上示例代码。 目录 引言 二、准备工作 你可以使用以下命令来安装这些库&a…

【pytorch】常用代码

文章目录 条件与概率torch.tensor()torch.rand()torch.randn()torch.randint()torch.multinominal() 逻辑运算torch.argmax()torch.max()torch.sum()torch.tanh()torch.pow() 功能性操作 torch.nn.functionalF.normalize()F.elu()F.relu()F.softmax() 张量计算torch.zeros()tor…

OpenHarmony分布式购物车案例展示~

简介 分布式购物车demo 模拟的是我们购物时参加满减活动&#xff0c;进行拼单的场景&#xff1b;实现两人拼单时&#xff0c;其他一人添加商品到购物车&#xff0c;另外一人购物车列表能同步更新&#xff0c;且在购物车列表页面结算时&#xff0c;某一人结算对方也能实时知道结…

苹果设备再现完美兼容32位软件 只需一款神奇工具 CrossOver 24发布:基于 Wine 9.0,能让 Mac 初步运行 32位应用

近日&#xff0c;CodeWeavers发布了CrossOver 24版本的更新。这次的更新是基于最新的Wine 9.0版本而进行的。这一版本的更新不仅能够兼容更多应用程序和游戏&#xff0c;而且还可以初步支持运行32位的应用程序。 自从苹果在macOS Catalina系统中移除对32位软件的支持之后&…

【Git】Git命令的学习与总结

本文实践于 Learn Git Branching 这个有趣的 Git 学习网站。在该网站&#xff0c;可以使用 show command 命令展示所有可用命令。你也可以直接访问网站的sandbox&#xff0c;自由发挥。 一、本地篇 基础篇 git commit git commit将暂存区&#xff08;staging area&#xff…

Outlook邮箱配置步骤?如何配置电子邮箱?

Outlook邮箱配置的方法&#xff1f;Outlook邮箱配置SMTP的方法&#xff1f; Outlook邮箱配置不仅能够帮助我们高效地管理邮件&#xff0c;还可以提供日程安排、联系人管理等多项功能。那么&#xff0c;如何配置Outlook邮箱呢&#xff1f;接下来&#xff0c;蜂邮EDM将为大家详细…

mac安装zookeeper

下载地址&#xff1a; http://archive.apache.org/dist/zookeeper/ 注意&#xff1a;由于Zookeeper从3.5.5版本开始&#xff0c;带有bin名称的包才是我们想要的下载可以直接使用的里面有编译后的二进制的包&#xff0c;而之前的普通的tar.gz的包里面是只是源码的包无法直接使…

HUAWEI Programming Contest 2024(AtCoder Beginner Contest 342)

D - Square Pair 题目大意 给一长为的数组&#xff0c;问有多少对&#xff0c;两者相乘为非负整数完全平方数 解题思路 一个数除以其能整除的最大的完全平方数&#xff0c;看前面有多少个与其余数相同的数&#xff0c;两者乘积满足条件&#xff08;已经是完全平方数的部分无…

微信小程序蓝牙通信HC08

总结这两天研究的蓝牙串口。人话版资料不多&#xff0c;主要靠翻别人的仓库和文档。 单片机部分&#xff0c;与蓝牙串口通信是通过串口。比我想的要简单&#xff0c;小程序部分&#xff0c;有非常多的服务和特征&#xff0c;而且人话版资料不多。 如果本文有什么问题&#xf…

AI之T2I:Stable Diffusion 3的简介、安装和使用方法、案例应用之详细攻略

AI之T2I&#xff1a;Stable Diffusion 3的简介、安装和使用方法、案例应用之详细攻略 目录 Stable Diffusion 3的简介 1、效果测试 官方demo 网友提供 Stable Diffusion 3的安装和使用方法 1、安装 2、使用方法 Stable Diffusion 3的案例应用 1、基础案例 Stable Diff…

RestTemplate启动问题解决

⭐ 作者简介&#xff1a;码上言 ⭐ 代表教程&#xff1a;Spring Boot vue-element 开发个人博客项目实战教程 ⭐专栏内容&#xff1a;个人博客系统 ⭐我的文档网站&#xff1a;http://xyhwh-nav.cn/ RestTemplate启动问题解决 问题&#xff1a;在SpringCloud架构项目中配…

Vue实现登录保存token并校验实现保存登录状态

文章目录 一、登录vue二、路由index 一、登录vue <script> import request from "/axios/baseURL"; import router from "/router";// 接口数据初始化 const FORM_DATA {userName: "",password: "", }; export default {data(…

腾讯文档(excel也一样)设置单元格的自动行高列宽

1. 选中单元格 可选择任意一个或者几个 2. 设置自动 行高和列宽 即可生效

掌握微信小程序开发的核心要点:从基础到进阶

文章目录 掌握微信小程序开发的核心要点&#xff1a;从基础到进阶一、数据绑定和事件处理1.1 理解小程序的数据绑定机制&#xff0c;实现数据和视图的同步更新1.2 学习如何处理用户交互事件和触发相应的响应逻辑 二、网络请求和数据交互2.1 使用小程序的网络请求API与后端服务器…

unity发布webGL压缩方式的gzip,使用nginx作为web服务器时的配置文件

unity发布webGL压缩方式的gzip&#xff0c;使用nginx作为web服务器时的配置文件 Unity版本是&#xff1a;2021.3 nginx的版本是&#xff1a;nginx-1.25.4 Unity发布webgl时的测试 设置压缩方式是gzip nginx配置文件 worker_processes 1;events {worker_connections 102…

vue项目打包获取git commit信息并输出到打包后的指定文件夹中

需求背景&#xff1a; 前端项目经常打包&#xff0c;发包部署&#xff0c;为了方便测试及运维发现问题时与正确commit信息对比 实现方式&#xff1a; 使用Node.js的child_process模块来执行git命令 实现步骤&#xff1a; 1.在package.json的同级目录下新建一个version.js文件。…

PyQt6的开发流程(密码生成小程序为例)

PyQt6的开发流程&#xff08;密码生成小程序为例&#xff09; 文章目录 PyQt6的开发流程&#xff08;密码生成小程序为例&#xff09;一、流程介绍与概览1. 界面与逻辑分离的开发流程2. PyQt6的开发流程 二、打开 designer.exe 创建文件三、用QT设计师绘制界面保存成ui1. QT常用…

springboot+vue网站开发-后端管理框架-vue-admin-template

为了方便国内用户下载&#xff0c;我把自己的百度网盘分享给大家一份地址&#xff0c;可以去下载。 如果你有上网盒子软件&#xff0c;那就自己去下载&#xff0c;很小。不到1MB. 链接&#xff1a;https://pan.baidu.com/s/15LJ2MoSWToFGFp28VaxBeQ?pwdbaby 提取码&#xff…

微服务-微服务链路追踪组件Skywalking实战

自动化监控系统Prometheus&Grafana实战&#xff1a; 4 trem APM-性能监控项目班&#xff1a; https://vip.tulingxueyuan.cn/detail/p_602e574ae4b035d3cdb8f8fe/6 1. skywalking是什么 1.1 Skywalking主要功能特性 1.2 Skywalking整体架构 1.3 SkyWalking 环境搭建部…