5

QQuickImageProvider我有一个图像提供程序派生自requestPixmap.

此图像提供程序适用于Image组件。

现在我想以同样的方式提供imageSourcefor a Button。但没有图像显示。可能是什么问题?

QML 代码

Image {
    anchors.fill: parent
    anchors.margins: 10
    source: "image://provider/" + model.DisplayRole
    fillMode: Image.PreserveAspectFit
}

Button {
    Layout.fillWidth: true
    Layout.preferredHeight: width
    iconSource: "image://provider/" + model.DisplayRole
    onClicked: appCore.switch(model.DisplayRole)
}

C++ 代码

QPixmap ImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
    QModelIndex index;
    bool foundId = false;

    for(int row = 0; row < m_myModel->rowCount(); row++)
    {
        index = m_myModel->index(row, 0);
        QString name = QVariant(m_myModel->data(index, Qt::DisplayRole)).toString();

        if(name == id)
        {
            foundId = true;
            break;
        }
    }

    if(!foundId)
        return QPixmap();

    QIcon icon = m_myModel->data(index, Qt::DecorationRole).value<QIcon>();

    QPixmap pixmap = icon.pixmap(128,128);

    return pixmap;;
}
4

0 回答 0