Qt 从imageprovider设置按钮imageSource
我有一个来自Qt 从imageprovider设置按钮imageSource,qt,qml,qtquick2,qtquickcontrols,Qt,Qml,Qtquick2,Qtquickcontrols,我有一个来自QQuickImageProvider的图像提供程序,它实现了requestPixmap 此图像提供程序可与图像组件配合使用 现在我想以同样的方式为按钮提供imageSource。但是没有图像显示出来。有什么问题吗 QML代码 Image { anchors.fill: parent anchors.margins: 10 source: "image://provider/" + model.DisplayRole fillMode: Image.P
QQuickImageProvider
的图像提供程序,它实现了requestPixmap
此图像提供程序可与图像组件配合使用
现在我想以同样的方式为按钮提供imageSource
。但是没有图像显示出来。有什么问题吗
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)
}
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;;
}
C++代码
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)
}
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;;
}
QPixmap图像提供者::requestPixmap(常量QString&id,QSize*size,常量QSize&requestedSize)
{
QModelIndex指数;
bool-foundId=false;
对于(int row=0;rowrowCount();row++)
{
index=m_myModel->index(行,0);
QString name=QVariant(m_myModel->data(index,Qt::DisplayRole)).toString();
if(name==id)
{
foundId=true;
打破
}
}
如果(!foundId)
返回QPixmap();
QIcon icon=m_myModel->data(index,Qt::DecorationRole).value();
QPixmap-pixmap=icon.pixmap(128128);
返回pixmap;;
}
请提供最低限度的工作项目来测试它。