Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

ImageProvider error



  • Hi,
    I have the following imageprovider setup:
    Main.qml:

    import Felgo 3.0
    import QtQuick 2.11
    import QtQuick.Dialogs 1.2
    
    App {
        id: app
    
        NavigationStack {
    
            Page {
                title: qsTr("Image Converter")
    
                IconButton {
                    id: enlarge
                    visible: false
                    color: "blue"
                    icon: IconType.arrowsalt
                    anchors.baseline: button.baseline
                    anchors.right: button.left
    
                    onClicked: {
                        PictureViewer.show(app, image.source)
                    }
                }
    
                AppButton {
                    id: button
    
                    anchors.horizontalCenter: imageViewer.horizontalCenter
                    anchors.bottom: imageViewer.top
                    text: "Choose Image"
                    onClicked: {
                        nativeUtils.displayImagePicker("Choose the Friend's Image")
                    }
                }
    
                Rectangle {
                    id: imageViewer
                    width: 250
                    height: 250
                    anchors.centerIn: parent
    
                    AppImage {
                        id: image
                        onStatusChanged: if (image.status == Image.Ready)
                                             enlarge.visible = true
                        anchors.fill: parent
                        width: 250
                        height: 250
                        autoTransform: true
                        fillMode: Image.PreserveAspectFit
                    }
                }
    
                Connections {
                    target: nativeUtils
    
                    onImagePickerFinished: {
                        if (accepted) {
                            var path
                            image.source = "image://pix/path"
    
                            console.log("The path in main.qml: ", path)
                        }
                    }
                }
            }
        }
    }
    
    

    MakePix.h:

    #ifndef MAKEPIX_H
    #define MAKEPIX_H
    
    #include <QObject>
    #include <QQuickImageProvider>
    #include <QString>
    
    class MakePix : public QObject, public QQuickImageProvider
    {
    	Q_OBJECT
    public:
    	MakePix()
    		: QQuickImageProvider(QQuickImageProvider::Pixmap)
    	{}
    
    	QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override;
    };
    
    #endif // MAKEPIX_H
    
    

    MakePix.cpp:

    #include "makepix.h"
    #include <QDebug>
    #include <QPixmap>
    
    QPixmap MakePix::requestPixmap(const QString& id, QSize* size, const QSize& requestedSize)
    {
    	QPixmap pixmap(id);
    	int height = pixmap.height();
    	int width = pixmap.width();
    	if(size)
    	{
    		*size = QSize(width, height);
    	}
    
    	QPixmap pixmap2(requestedSize.width() > 0 ? requestedSize.width() : width,
    					requestedSize.height() > 0 ? requestedSize.height() : height);
    
    	return pixmap2;
    }
    
    

    Main.cpp:

    #include "makepix.h"
    #include <FelgoApplication>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QQuickView>
    
    int main(int argc, char* argv[])
    {
    	QApplication app(argc, argv);
    	FelgoApplication felgo;
    
    	// Use platform-specific fonts instead of Felgo's default font
    	felgo.setPreservePlatformFonts(true);
    
    	QQmlApplicationEngine engine;
    	felgo.initialize(&engine);
    
    	felgo.setMainQmlFileName(QStringLiteral("qml/Main.qml"));
    
    	QQuickView view;
    	view.engine()->addImageProvider(QLatin1String("pix"), new MakePix);
    	view.setSource(QUrl::fromLocalFile(QStringLiteral("Main.qml")));
    	view.show();
    
    	engine.load(QUrl(felgo.mainQmlFileName()));
    
    	return app.exec();
    }
    
    

    It builds without any problems, but when I run it it gives the following error message:
    "Main.qml:43:17: QML AppImage: Invalid image provider: image://pix/path".
    How can I correct this error?
    Thank you for your help.


  • Lifetime Qt Champion

    Hi,

    I haven't used Felgo but one thing that strikes me is that you might be using two different engines. One of which you didn't configure to use your image provider.



  • @SGaist ,
    WHich one is the unconfigured one?


  • Lifetime Qt Champion

    Well... The one you don't call addImageProvider on.



  • @SGaist ,
    I made some changes:

    #include "makepix.h"
    #include <FelgoApplication>
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QQuickView>
    
    int main(int argc, char* argv[])
    {
    	QApplication app(argc, argv);
    	FelgoApplication felgo;
    
    	felgo.setPreservePlatformFonts(true);
    
    	QQmlApplicationEngine engine;
    	felgo.initialize(&engine);
    
    	felgo.setMainQmlFileName(QStringLiteral("qml/Main.qml"));
    
    	MakePix* myMakePix = new MakePix();
    
    	engine.addImageProvider("pix", myMakePix);
    
    	engine.load(QUrl(felgo.mainQmlFileName()));
    
    	return app.exec();
    }
    
    

    Now the image provider works. Thank you for your help.


Log in to reply