[Solved] Wanted: QQuickImageProvider examples
-
Hey Guys,
I want to use QPixmaps in QML-Gui. For example:
@interface->setProperty("image",QPixmap(path);@After some research I guess I should use QQuickImageProvider, but in the documentations the examples are missing: "QQuickImageProvider":http://qt-project.org/doc/qt-5.0/qtquick/qquickimageprovider.html
Does somebody know a good example to use it?Thank you very much!
-
Basically you implement one of
@virtual QImage requestImage(const QString & id, QSize * size, const QSize & requestedSize)
virtual QPixmap requestPixmap(const QString & id, QSize * size, const QSize & requestedSize)
virtual QQuickTextureFactory * requestTexture(const QString & id, QSize * size, const QSize & requestedSize)
@
Create your provider setting type to one you implemented and add to qmlengine:
@QQmlEngine::addImageProvider(QLatin1String("idForYourProvider"), new YourProvider(type));@Then you can use it from Qml using "image" schema with your id:
@Image { source: "image://idForYourProvider/imageFromYourProvider.png" }@
Edit:
Seems like 4.8 documentation has the example:
http://qt-project.org/doc/qt-4.8/qdeclarativeimageprovider.html -
Thank you for your help. I wrote something like this (doesnt make sense, only wanted to test it):
@#include <QColor>
class ImageProvider : public QQuickImageProvider
{
public:
ImageProvider()
: QQuickImageProvider(QQuickImageProvider::Pixmap)
{
}QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) { int width = 160; int height = 100; if(size) *size = QSize(width,height); QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width, requestedSize.height() > 0 ? requestedSize.height() : height); pixmap.fill(QColor("blue").rgba()); return pixmap; }
};
@Gui.cpp:
@
engine = new QQmlEngine;
engine->addImageProvider(QLatin1String("provider"), new ImageProvider);
viewer = new QQuickView;
viewer->setSource(QUrl::fromLocalFile("qml/QML-MRGalleyServer/main.qml"));
viewer->show();
interface = viewer->rootObject();
interface->setProperty("lastpicture7","image://provider/blue.png");
@The property "lastpicture7" links to a source of an image. But I get following error: QML Image: Invalid image provider: image://provider/blue.png. Shoudnt it work this way? =( I used the 4.8 Example for QDeclarativeImageProvider
-
You are not using engine you created at fist line. And also documentation says that you have to add image provider before loading qml so your code in Gui.cpp should be:
@ viewer = new QQuickView;
viewer->engine()->addImageProvider(QLatin1String("provider"), new ImageProvider);
viewer->setSource(QUrl::fromLocalFile("qml/QML-MRGalleyServer/main.qml"));
viewer->show();
interface = viewer->rootObject();
interface->setProperty("lastpicture7","image://provider/blue.png");@ -
You could take a look at "this wiki page":http://www.developer.nokia.com/Community/Wiki/Multifunctional_Image_Tool:_A_base_for_image_interaction#Image_Handler.
It could be helpful.
-
Thank you very much! The provider works perfect now. But i still have a little problem:
The pixmaps provided by the ImageProvider have to change in runtime. So I call setProperty more than once. But when I change the property for the second time it doesnt call "requestPixmap". In the documentation it says:
Image caching
Images returned by a QDeclarativeImageProvider are automatically cached, similar to any image loaded by the QML engine. When an image with a "image://" prefix is loaded from cache, requestImage() and requestPixmap() will not be called for the relevant image provider. If an image should always be fetched from the image provider, and should not be cached at all, set the cache property to false for the relevant Image, BorderImage or AnimatedImage object.But I've got the same problems when I use @cache: false@
for the relevant Image. Does anyone have an idea?