0

我正在使用 QQuickImageProvider 并在 requestimage 函数中使用了一个类对象(PageBuffer):

class ImageProvider : public QQuickImageProvider{
public:
explicit ImageProvider();
virtual QImage requestImage(int id, QSize *size, const QSize& requestedSize,PageBuffer p);
~ImageProvider();
 };

我想使用保存在 PageBuffer 对象中的变量设置 ImageProvider 的 QImage 并使用 id 作为索引 像这样:

QImage ImageProvider:: requestImage(int id, QSize *size, const QSize &requestedSize,PageBuffer p) {
return p.imagelist[id];
}

这是我要调用图像提供程序的 QML 文件:

               Image{
                    x: 4
                    y: 4
                    height : imagerec.height
                    visible: true
                    width : imagerec.width
                    anchors.fill: imagerec
                    source:fileUrl
                    Text{
                        id:txt
                        x: 0
                        y: 71
                        text:"Sketch"+(index+1)
                        horizontalAlignment: txt.AlignHCenter
                        font.family: "Tahoma"
                        color:"#ffffff"

                    }

                    MouseArea {
                        anchors.rightMargin: -59
                        anchors.bottomMargin: -39
                        anchors.fill: parent
                        onClicked: {
                            p.index=index;
                            p.image=mod.get(index).fileUrl
                            main.source="image://image/"+index
                        }
                    }
                }
4

1 回答 1

4

好的,你们可以从Qt的这个例子开始。下面的代码也演示了如何使用 Image Provider 从 Internet 加载随机图像,尽管我认为这很简单。

myimageprovider.h

class MyImageProvider : public QQuickImageProvider
{
public:
    MyImageProvider(ImageType type, Flags flags = 0);
    ~MyImageProvider();
    QImage requestImage(const QString & id, QSize * size, const QSize & requestedSize);

protected:
    QNetworkAccessManager *manager;
};

myimageprovider.cpp

MyImageProvider::MyImageProvider(ImageType type, Flags flags) :
    QQuickImageProvider(type,flags)
{
    manager = new QNetworkAccessManager;
}

MyImageProvider::~MyImageProvider()
{
    delete manager;
}

QImage MyImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
    QUrl url("http://lorempixel.com/" + id);
    QNetworkReply* reply = manager->get(QNetworkRequest(url));
    QEventLoop eventLoop;
    QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec();
    if (reply->error() != QNetworkReply::NoError)
        return QImage();
    QImage image = QImage::fromData(reply->readAll());
    size->setWidth(image.width());
    size->setHeight(image.height());
    return image;
}

在 main.cpp 中注册我们的图像提供程序:

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    MyImageProvider *imageProvider = new MyImageProvider(QQmlImageProviderBase::Image);
    engine.addImageProvider("myprovider",imageProvider);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}

最后在 QML 中使用提供者:

Window {
    visible: true
    width: 800
    height: 600

    Image {
        id: img
        anchors.centerIn: parent
        source: "image://myprovider/500/500/"
        onStatusChanged: {
            if(status == Image.Ready)
                indicator.running = false;
        }

        BusyIndicator {
            id: indicator
            anchors.centerIn: parent
            running: false
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                indicator.running = true;
                img.source = "image://myprovider/500/500/?seed=" + Math.random(1000)
            }
        }
    }
}
于 2015-04-29T00:04:31.487 回答