• qml使用c++自定义listmodel数据


    qml要使用c++中自定义的model,首先该model类需要继承QAbstractListModel类,然后需要重写其中的三个函数,分别是
    int rowCount(const QModelIndex &parent);
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole);
    QHash roleNames();

    代码如下:
    DriverModel.h

    #ifndef DRIVERMODEL_H
    #define DRIVERMODEL_H
    
    
    #include 
    #include 
    #include 
    
    
    class DriverModel : public QAbstractListModel
    {
        Q_OBJECT
    public:
    
        typedef struct DriverData{
            QString type;
            QString desc;
            QString version;
        }DriverData;
    
        static DriverModel *instance(QObject *parent = nullptr) {
            static DriverModel *instance = nullptr;
            static std::mutex instance_mutex;
            if (instance == nullptr) {
                std::lock_guard<std::mutex> guard(instance_mutex);
                if (instance == nullptr) {
                    instance = new DriverModel(parent);
                }
            }
            return instance;
        }
    
        explicit DriverModel(QObject *parent = nullptr){
            appendItem(QString::fromLocal8Bit("显卡"),"Raden RX220","1.25.0.1");
        }
    
        enum LIST_ITEM_ROLE
        {
            type = Qt::UserRole+1,
            desc,
            version
        };
    
    
    public:
        //必须重写三个函数
        int rowCount(const QModelIndex &parent) const override
        {
            Q_UNUSED(parent);
            return m_list.count();
        }
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
        {
            int row = index.row();
            if(row < 0 || row >= m_list.count()) {
                return QVariant();
            }
    
            switch(role)
            {
            case type:
                return m_list.value(row).type;
            case desc:
                return m_list.value(row).desc;
            case version:
                return m_list.value(row).version;
            default:
                break;
            }
    
            return QVariant();
        }
        QHash<int,QByteArray> roleNames() const override
        {
            QHash<int, QByteArray> roles;
            roles[desc] = "desc";
            roles[type] = "type";
            roles[version] = "version";
            return roles;
        }
    
    public:
        //追加数据
        void appendItem(const QString& type,const QString& desc,const QString& version)
        {
            DriverData data;
            data.type = type;
            data.desc = desc;
            data.version = version;
    
            beginInsertRows(QModelIndex(), m_list.size(), m_list.size());
            m_list.append(data);
            endInsertRows();
        }
        //清空模型数据
        void clearModelData() {
            beginResetModel();
            m_list.clear();
            endResetModel();
        }
    
    
    
    private:
        QList<DriverData> m_list;
    
    };
    
    
    
    #endif // DRIVERMODEL_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112

    main.cpp

    #include 
    #include 
    #include "DriverModel.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        engine.rootContext()->setContextProperty("driverModel",DriverModel::instance());
    
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    
        engine.load(url);
    
        return app.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    main.qml

    import QtQuick 2.12
    import QtQuick.Window 2.12
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        ListView{
            width: parent.width
            height: parent.height
            visible: true
            model: driverModel
            delegate: Item{
                Text{
                    id:text1
                    text: type
                }
                Text{
                    id:text2
                    text: desc
                    anchors.top: text1.bottom
                }
                Text{
                    text: version
                    anchors.top: text2.bottom
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
  • 相关阅读:
    2023亚太杯数学建模思路 - 案例:最短时间生产计划安排
    常见的SSH功能
    攻防世界----favorite_number
    Python(12)进程与线程
    【解救ROS】ros小车机器人摄像头寻线的实现(基于opencv)
    Git创建、diff代码、回退版本、撤回代码,学废了吗
    jvm08
    网络—网络通信基础(理论)
    卷积神经网络激活层作用,卷积神经网络激活函数
    ESB优势2019-架构师(六十二)
  • 原文地址:https://blog.csdn.net/bangtanhui/article/details/133420104