当前位置: 首页 > news >正文

qml刷新C++中的QImage图像

第一步:重写QQuickImageProvider类

#include <QQuickImageProvider>class CQuickImagePainter : public QQuickImageProvider
{
public:CQuickImagePainter();QImage requestImage(const QString&id,   QSize *, const QSize &);QPixmap requestPixmap(const QString&id, QSize *, const QSize &);public:QImage img;
};
#include "CQuickImagePainter.h"CQuickImagePainter::CQuickImagePainter(): QQuickImageProvider(QQuickImageProvider::Image)
{}QImage CQuickImagePainter::requestImage(const QString&id, QSize*, const QSize&)
{return this->img;
}QPixmap CQuickImagePainter::requestPixmap(const QString&id, QSize*, const QSize&)
{return QPixmap::fromImage(this->img);
}

第二步:定义刷新图像的类

#include <QObject>
#include <CQuickImagePainter.h>class CQuickImageRefresh : public QObject
{Q_OBJECT
public:explicit CQuickImageRefresh(QObject *parent = nullptr);CQuickImagePainter*m_RePainter;signals:void reDrawImage();public slots:void setImage(QImage image);
};
#include "CQuickImageRefresh.h"CQuickImageRefresh::CQuickImageRefresh(QObject *parent) : QObject(parent)
{m_RePainter = new CQuickImagePainter;
}void CQuickImageRefresh::setImage(QImage image)
{m_RePainter->img = image;emit reDrawImage();
}

第三步:关联qml

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "CQuickImageRefresh.h"//设置为全局变量-可以在线程中更新图像
CQuickImageRefresh *CodeImage1 = new CQuickImageRefresh();
int main(int argc, char *argv[])
{QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);QGuiApplication app(argc, argv);QQmlApplicationEngine engine;const QUrl url(QStringLiteral("qrc:/main.qml"));QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,&app, [url](QObject *obj, const QUrl &objUrl) {if (!obj && url == objUrl)QCoreApplication::exit(-1);}, Qt::QueuedConnection);engine.load(url);//管理qmlengine.rootContext()->setContextProperty("CodeImage1",CodeImage1);engine.addImageProvider(QLatin1String("CodeImg1"), CodeImage1->m_RePainter);//每次CodeImage1->setImage(image)的时候界面自动刷新QImage image = QImage(data,width,height,QImage::Format_RGB888);CodeImage1->setImage(image);return app.exec();
}

第四步:qml显示

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12Window {width: 640height: 320visible: truetitle: qsTr("Hello World")Image{id:img1width: 640height: 360x:0y:0}Connections{target: CodeImage1onReDrawImage:{img1.source = ""img1.source = "image://CodeImg1"}}
}
http://www.lryc.cn/news/258174.html

相关文章:

  • IJCAI 2024 International Joint Conference on Artificial Intelligence
  • 使用Python Flask搭建Web问答应用程序并发布到公网远程访问
  • android 13.0 app应用安装白名单
  • SSL证书HTTPS保护服务
  • 快速认识什么是:Docker
  • c语言青蛙跳台阶
  • IntelliJ IDEA 2023.3 最新版如何试用?IntelliJ IDEA 2023.3 最新版试用方法
  • Java参数校验详解:使用@Valid注解和自定义注解进行参数验证
  • 多维时序 | MATLAB实现BWO-CNN-BiGRU-Multihead-Attention多头注意力机制多变量时间序列预测
  • C++ 中的引用
  • MQ-Det: Multi-modal Queried Object Detection in the Wild
  • HarmonyOS应用开发初体验
  • 《C++新经典设计模式》之第4章 策略模式
  • 【方法】PowerPoint“只读方式”如何取消?
  • MySQL数据库概念与实践
  • 【ArcGIS Pro微课1000例】0052:基于SQL Server创建企业级地理数据库案例
  • 深度学习——第3章 Python程序设计语言(3.7 matplotlib库)
  • 【数据分析实战】酒店行业华住集团门店分布与评分多维度分析
  • 近期Chrome浏览器 不知哪个版本升级后原先http强制跳转到https,导致服务端302强制跳转到http也没反应
  • 【scikit-learn基础】--『数据加载』之样本生成器
  • 基于 ESP32-S3 的 Walter 开发板
  • Gitlab+GitlabRunner搭建CICD自动化流水线将应用部署上Kubernetes
  • 待做-待补充-每个节点做事,时间,以及与角度的关系
  • 液态二氧化碳储存罐远程无线监测系统
  • kafka学习笔记--安装部署、简单操作
  • UE4 材质实现Glitch效果
  • oracle实验2023-12-8--触发器
  • 【Python百宝箱】贝叶斯统计的魅力:从PyMC3到ArviZ,探索数据背后的不确定性
  • Knowledge Graph知识图谱—8. Web Ontology Language (OWL)
  • 排序算法——冒泡排序