avb*_*avb 5 qt qml qtquick2 qtquickcontrols
我有一个图像提供程序派生自QQuickImageProvider实现requestPixmap.
此图像提供程序适用于Image组件.
现在我想以同样的方式提供imageSourcea 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)
}
Run Code Online (Sandbox Code Playgroud)
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;;
}
Run Code Online (Sandbox Code Playgroud)