C++給QML傳QImage

很簡單,用QQuickImageProvider這個類。下面貼代碼。

環境QT5.9
項目使用Quick Controls 2Application

項目比較簡單。就不多做介紹了。注意頭文件的使用。


頭文件img.h


#ifndef IMG_H
#define IMG_H
#include <QQuickImageProvider>
#include<QImage>
#include<QTcpSocket>
#include <QTcpServer>
#include<QBuffer>
class ImageProvider : public QQuickImageProvider
{
public:
    ImageProvider();
    QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
    QImage img;
};
class ShowImage : public QObject
{
    Q_OBJECT
public:
    explicit ShowImage(QObject *parent = 0);
    ImageProvider *m_pImgProvider;
public slots:
    void sendimage(QImage);
signals:
    void callQmlRefeshImg();
    void sendPic(QImage image);
};




#endif // IMG_H

main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "img.h"
#include <QQmlContext>
#include <QtWidgets/QApplication>
#include <QScreen>
#include <QtWidgets/QWidget>
#include <QImage>
ImageProvider::ImageProvider()
    : QQuickImageProvider(QQuickImageProvider::Image)
{
}

QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
    return this->img;
}

QPixmap ImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
    return QPixmap::fromImage(this->img);
}

ShowImage::ShowImage(QObject *parent) :
    QObject(parent)
{
    m_pImgProvider = new ImageProvider();
}

void  ShowImage::sendimage(QImage sendimage)
{
      m_pImgProvider->img =sendimage ;
      emit callQmlRefeshImg(); //告訴qml刷新一下。
}

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);  //注意qapplication qguiapplication
    QQmlApplicationEngine engine;
   ShowImage *CodeImage=new ShowImage();
   engine.rootContext()->setContextProperty("CodeImage",CodeImage);
   engine.addImageProvider(QLatin1String("CodeImg"),CodeImage->m_pImgProvider);
   engine.load(QUrl(QLatin1String("qrc:/main.qml")));
   QImage image;
   image.load("d:\\q.jpg");  //載入
   CodeImage->sendimage(image);//發送
    if (engine.rootObjects().isEmpty())
        return -1;
    return app.exec();
}






QML這裏注意下
targrt:Codeimage就是
engine.rootContext()->setContextProperty("CodeImage",CodeImage);
這句話註冊的。
值得提醒的是。
onCallQmlRefeshImg:這裏,我在c++裏開頭首字母寫的是小寫,這裏用大寫是可以的,但是如果c++函數那裏寫的是大寫,qml這裏不管你是小寫還是大寫都報錯。官方文檔裏只是寫了on<singel>,沒說大小寫。很奇怪。反正就是c++那裏首字母小寫,qml裏大寫就對了。


main.qml:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Image
    {
        id:img
        anchors.fill: parent
    }
        Connections{
            target: CodeImage;  //on{singal}: {//}
            onCallQmlRefeshImg:
            {
                        img.source =""
                        img.source = "image://CodeImg"
            }
        }
    
}

最終效果






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章