i want to send the dynamic image to qml and display on Qt gui i read online image provider is way to solve this issue but i want any sample and simple application which does this.
Asked
Active
Viewed 753 times
0
-
The approach that I use to show png images, was using `QLabel` and enabling `pixmap`, after this just selecting the png resource. – joaopedro Jan 25 '21 at 11:47
-
Does this answer your question? [Qt/QML : Send QImage From C++ to QML and Display The QImage On GUI](https://stackoverflow.com/questions/20691414/qt-qml-send-qimage-from-c-to-qml-and-display-the-qimage-on-gui) – Farshid616 Jan 25 '21 at 12:07
-
@Farshid616 i gone through that link as i am beginner to QT i am not much clear, its better if i get any reference code. – Aliana ford Jan 25 '21 at 12:24
-
Here's an [example](https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/quick/imageprovider?h=5.15). The explanation is [here](https://doc.qt.io/qt-5/qquickimageprovider.html#an-example). – JarMan Jan 25 '21 at 13:07
1 Answers
1
You should create an image provider class where you load your image, this is a simplified example.
qmlimageprovider.h
#ifndef QMLIMAGEPROVIDER_H
#define QMLIMAGEPROVIDER_H
#include <QQuickImageProvider>
class QmlImageProvider : public QQuickImageProvider
{
public:
QmlImageProvider();
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
void updateImage(QImage new_image);
private:
QImage image;
};
#endif // QMLIMAGEPROVIDER_H
qmlimageprovider.cpp
#include "qmlimageprovider.h"
QmlImageProvider::QmlImageProvider()
: QQuickImageProvider(QQuickImageProvider::Image)
{
}
QImage QmlImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
image.load("/home/ubuntu/music.jpg");
return image;
}
void QmlImageProvider::updateImage(QImage new_image)
{
image = new_image;
}
Then you should register this class to your QML engine.
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "qmlimageprovider.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.addImageProvider(QLatin1String("imageprovider"),
new QmlImageProvider());
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);
return app.exec();
}
Finally you can use imageprovider in your qml page and don't forget to write image reload function because images will be cache and you need to reload in order to change them.
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.3
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Connections {
target: imageprovider
onNewFrameReceived: image.reload();
}
Item {
id: name
width: parent.width
height: parent.height
Rectangle
{
id: progressBackground
height: parent.height
width: parent.width
Text {
id: text
text: qsTr("click to show")
color: "#c400c4"
}
MouseArea {
id: progressArea
anchors.fill: progressBackground
onClicked:
{
image.source = "image://imageprovider/cover"
}
}
}
Image {
id: image
anchors.left: text.right
source: ""
cache: false
function reload() {
var oldSource = source;
source = "";
source = oldSource;
console.log("reload")
}
}
}
}
You can call imageprovider update function to change the image.

Farshid616
- 1,404
- 1
- 14
- 26
-
Thanks but while compiling this source code i am facing error : QQmlApplicationEngine failed to load component qrc:/main.qml:-1 File not found I am using QT 5.12.9 version – Aliana ford Jan 27 '21 at 11:32
-
You should add a resource file to your project, then add main.qml to your resource file. Follow this answer for more information https://stackoverflow.com/a/41578261/3365136 – Farshid616 Jan 27 '21 at 11:36
-
Thanks Thanks a lot for you help. Instead of image.load("/home/ubuntu/music.jpg"); function i am trying to read the image from socket. but it showing me error – Aliana ford Jan 28 '21 at 10:14
-
Please ask another question about read image from socket and community will answer you because it's a new question outside of this topic. – Farshid616 Jan 28 '21 at 11:40