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);

   //管理qml
   engine.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.12


Window {
    width: 640
    height: 320
    visible: true
    title: qsTr("Hello World")

    Image{
        id:img1
        width: 640
        height: 360
        x:0
        y:0
    }
    Connections{
        target: CodeImage1
        onReDrawImage:{
            img1.source = ""
            img1.source = "image://CodeImg1"
        }
    }
}

相关推荐

  1. qml刷新C++QImage图像

    2023-12-10 07:10:03       63 阅读
  2. QTQImage与QPixmap区别

    2023-12-10 07:10:03       48 阅读
  3. Qt图像处理-OpenCvMat与QImage互转

    2023-12-10 07:10:03       56 阅读
  4. c++ opencvunsigned char *、Mat、Qimage互相转换

    2023-12-10 07:10:03       61 阅读
  5. QImage加载opencv读取图片出错

    2023-12-10 07:10:03       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-10 07:10:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-10 07:10:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-10 07:10:03       82 阅读
  4. Python语言-面向对象

    2023-12-10 07:10:03       91 阅读

热门阅读

  1. 使用Spring Security、JWT和Swagger进行登录验证的流程

    2023-12-10 07:10:03       54 阅读
  2. Tomcat使用https方式连接

    2023-12-10 07:10:03       53 阅读
  3. 【MySQL】之联合索引与最左匹配原则

    2023-12-10 07:10:03       45 阅读
  4. 动态规划01-斐波那契类型一

    2023-12-10 07:10:03       54 阅读
  5. 安卓11双屏双背光修改方法

    2023-12-10 07:10:03       57 阅读
  6. Python 实现全连接攻击

    2023-12-10 07:10:03       65 阅读
  7. ffmpeg编译问题

    2023-12-10 07:10:03       53 阅读
  8. 企业如何挑选适合自己的外呼机器人供应商?

    2023-12-10 07:10:03       59 阅读