0
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);

PS对不起我的英语。

~解决了~

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

有用!非常感谢你!

4

1 回答 1

1

可能Image使用 theImageProvider没有正确声明其宽度和高度? Columns 对没有声明其宽度和高度的项目有点好笑。尝试在第二个示例中对widthandheight进行硬编码,它应该可以工作。Image

于 2011-04-28T07:02:30.677 回答