【问题标题】:Bind a QIcon provided by a class derived from QAbstractListModel to QML Image将派生自 QAbstractListModel 的类提供的 QIcon 绑定到 QML Image
【发布时间】:2014-02-18 10:26:37
【问题描述】:

我尝试将由 QAbstractListModel 派生的类提供的 QIcon 绑定到 QML Image,如下所示:

Component {
    id: myDelegate
    //...
            Column {
                anchors.verticalCenter: parent.verticalCenter
                spacing: 5

                Image {
                    source: model.DecorationRole
                }

                Text {
                    text: model.DisplayRole
    //...
}

但这会导致这个错误:

无法将 QIcon 分配给 QUrl

如何正确地做到这一点?

【问题讨论】:

    标签: c++ qt qml qt-quick qabstractitemmodel


    【解决方案1】:

    您不能将 QIcon 指定为 QML Image 的来源。

    您需要为您的图标选择自定义 URL 格式,例如

    images://myicons/<icon_id>
    

    此字符串是您设置为Image.source 的 URL。

    现在您创建并注册一个 imageprovider,它会在您从 QML 发送请求的 URL 时提供图标:

    • 创建QQuickImageProvider 的子类,例如MyIconProvider
    • 覆盖以id为参数并返回像素数据的函数requestPixmap

    然后你在你的main.cpp注册图片提供者

    MyIconProvider *mip = new MyIconProvider();
    engine.addImageProvider("myicons", mip); 
    

    【讨论】:

    • 能否详细说明如何返回像素数据(显然必须来自我的模型)?
    【解决方案2】:

    我想出了这个解决方案。它有效,但非常欢迎任何建议或改进。

    ImageProvider.h

    class ImageProvider : public QQuickImageProvider
    {
        public:
            explicit ImageProvider(myModel *myModel);
            QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize);
    
        signals:
    
        public slots:
    
        private:
            myModel *m_myModel;
    };
    

    ImageProvider.cpp

    ImageProvider::ImageProvider(myModel *myModel) :
        QQuickImageProvider(QQuickImageProvider::Pixmap),
        m_myModel(myModel)
    {
    }
    
    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;
    }
    

    注册 imageProvider...

    ImageProvider *imageProvider = new ImageProvider(myModel);
    view->engine()->addImageProvider(QLatin1String("provider"), imageProvider);
    

    ma​​in.qml

    Component {
        id: myDelegate
        //...
                Column {
                    anchors.verticalCenter: parent.verticalCenter
                    spacing: 5
    
                    Image {
                        source: "image://provider/" + model.DisplayRole
                    }
    
                    Text {
                        text: model.DisplayRole
        //...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多