How to display a QPixmap/QImage in QML?
-
@Wieland Thanks a lot for your code example. This was very interresting, but still to much overhead for my very simple use case!
I found an alternative solution, which works fine for me. I post it as reply to my question if someone else has the same needs.
To pass the QImage to QML, I simply serialize into a QByteArray like this:Q_PROPERTY(QString picture READ picture CONSTANT) ... const QString MyImageClasse::picture() { QImage myImage; // Some init code to setup the image (e.g. loading a PGN/JPEG, etc.) QByteArray bArray; QBuffer buffer(&bArray); buffer.open(QIODevice::WriteOnly); myImage.save(&buffer, "JPEG"); QString image("data:image/jpg;base64,"); image.append(QString::fromLatin1(bArray.toBase64().data())); return image; } ...
Works for me (TM) ;-)
Regards
-
@Wieland said in How to display a QPixmap/QImage in QML?:
qmlRegisterType<PixmapContainer>("io.qt.forum", 1, 0, "PixmapContainer");
qmlRegisterType<Backend>("io.qt.forum", 1, 0, "Backend");
qmlRegisterType<PixmapImage>("io.qt.forum", 1, 0, "PixmapImage");@Wieland Why did you register the backend & container objects? Why would you access such objects in the QML? Just being curious...
-
@Julian-Guarin You're right, registering those wasn't necessary here.
-
This is a very good example. I follow the code and it works exactly as I expected.
Thank you so much -
Hi, I just copied this example but I got some errors. in "backend.cpp" and "pixmapimage.cpp" . The errors are the same and:
error: undefined reference to `PixmapContainer::PixmapContainer(QObject*)'
What is the problem?
-
@yunus said in How to display a QPixmap/QImage in QML?:
What is the problem?
The problem is right there in the error message.
Where is PixmapContainer::PixmapContainer(QObject*) defined?
I guess you forgot to add the cpp file for PixmapContainer.Or change this line
explicit PixmapContainer(QObject *parent = 0);
to
explicit PixmapContainer(QObject *parent = 0) : QObject(parent) {}
-
@KroMignon It works, thank you!
-
@KroMignon said in How to display a QPixmap/QImage in QML?:
I found an alternative solution
So are you feeding that string to the "source" property of QML Image?
-
Thank a lot for your code example. Really appreciate.