QDeclarativeImageProvider不会在容器中加载图像

时间:2011-04-28 05:27:40

标签: c++ qt qml

Image {
        id: backImage
        source: "image://imageprovider/frontimage"
    }

工作正常,但不适用于Column或其他容器

Column {

    Image {
            id: backImage
            source: "image://imageprovider/frontimage"
        }
}

,而

Column {

    Image {
            id: backImage
            source: "screen1.png"
        }
}

行。 为什么呢?

声明:

class QMLImageProvider : public QObject, public QDeclarativeImageProvider
{
    Q_OBJECT
private:
    QPixmap front;
    QPixmap back;

public:
    explicit QMLImageProvider();
    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
};

实现:

QMLImageProvider::QMLImageProvider() : QObject(0),
        QDeclarativeImageProvider(QDeclarativeImageProvider::Pixmap) {}

QPixmap QMLImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) {
    return QPixmap(QString("QML/screen1.png"));
}

注册:

screenArea = new QDeclarativeView(wgScreenArea);
    qmlImageProvider = new QMLImageProvider();
    screenArea->engine()->addImageProvider(GlobalVars::imageProviderID, qmlImageProvider);

    screenArea->setSource(QUrl::fromLocalFile("QML/screen.qml"));
    screenArea->setResizeMode(QDeclarativeView::SizeRootObjectToView);

P.S。抱歉我的英文。

〜解决〜

Column {
    id:screenImage
    Image {
        id: backImage
        width: screenImage.width
        height: screenImage.height
        source: "image://imageprovider/backimage"
    }
}

有效! 非常感谢你!

1 个答案:

答案 0 :(得分:1)

使用Image的{​​{1}}可能无法正确声明其宽度和高度? ImageProvider对于没有声明宽度和高度的项目有点搞笑。在第二个示例中尝试对Columnwidth中的heightImage进行硬编码,它应该有效。