How to display a combobox dynamically, so that each combobox corresponds to a specific idModel?



  • How to display a combobox dynamically, so that each combobox corresponds to a specific idModel? I have ThreeDModelTexture a
    Problem code on github

    QAbstractListModel descendant that has roles:

        enum ThreeDModelRolse {
            nameRole = Qt::DisplayRole,
            idModelRole = Qt::UserRole + 1,
            urlRole = Qt::UserRole + 2
        };
      roles[idModelRole] = "idModel"; roles[nameRole] = "name"; roles[urlRole] = "url";
    

    0_1554631365193_tabelENG.jpg

    Fully class

    threedmodeltexture.h

    #ifndef THREEDMODELTEXTURE_H
    #define THREEDMODELTEXTURE_H
     
    #include <QString>
    #include <QObject>
    #include <QString>
    #include <QAbstractListModel>
    #include <QList>
    #include <QVariant>
    #include <QModelIndex>
    #include "baseqmlabstractlistmodel.h"
    class Texture;
     
    class ThreeDModelTexture: public QAbstractListModel
    {
        Q_PROPERTY(int _idModel READ idmodel(int) )
        Q_PROPERTY(QString _name READ name(int) )
        Q_PROPERTY(QString _url READ url(int) )
    public:
        ThreeDModelTexture(QObject *parent = nullptr);
        ~ThreeDModelTexture();
        enum ThreeDModelRolse {
            nameRole = Qt::DisplayRole,
            idModelRole = Qt::UserRole + 1,
            urlRole = Qt::UserRole + 2
        };
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
        Q_INVOKABLE int rowCount(const QModelIndex &parent) const; // количество элементов в модели
        void addTexture(Texture tex)const;
        Q_INVOKABLE static int Count();
        Q_INVOKABLE static int boolMatchSearch(int id);
     
        Q_INVOKABLE int idmodel(const int &id);
        Q_INVOKABLE QString name(const int &id);
        Q_INVOKABLE QString url(const int &id);
     
    public slots:
        QHash<int, QByteArray> roleNames() const;
        Q_INVOKABLE static QList<Texture> textures;
        QHash<int, QByteArray> roles; // роли
    };
     
    class Texture
    {
    public:
        Texture(int _idModel, QString _name, QString _url)
            :idModel(_idModel)
            , name(_name)
            , url(_url)
        {
    //        idModel = _idModel;
    //        name = _name;
    //        url = _url;
        }
        void setName (QString _name)
        {
            name = _name;
        }
        void seturl (QString _url)
        {
            url = _url;
        }
        void setidModel (int _idModel)
        {
            idModel = _idModel;
        }
        QString getName() const
        {
            return name;
        }
        QString getUrl() const
        {
            return url;
        }
        int getIDModel() const
        {
            return idModel;
        }
    private:
        int idModel;
        QString name;
        QString url;
     
    };
     
    #endif // THREEDMODELTEXTURE_H
    

    threedmodeltexture.cpp

    #include "threedmodeltexture.h"
    #include <QDebug>
    ThreeDModelTexture::ThreeDModelTexture(QObject *parent) :
        QAbstractListModel(parent)
    {
        roles[idModelRole] = "idModel";
        roles[nameRole] = "name";
        roles[urlRole] = "url";
    }
     
    QList<Texture> ThreeDModelTexture::textures;
     
    ThreeDModelTexture::~ThreeDModelTexture()
    {
     
    }
     
    QVariant ThreeDModelTexture::data(const QModelIndex &index, int role) const
    {
        if (index.row() < 0 || index.row() > ThreeDModelTexture::textures.count())
            return QVariant();
        const Texture tex = ThreeDModelTexture::textures[index.row()];
        if (role == idModelRole)
            return tex.getIDModel();
        else if (role == nameRole)
            return tex.getName();
        else if (role == urlRole)
            return tex.getUrl();
        return QVariant();
    }
     
    QHash<int, QByteArray> ThreeDModelTexture::roleNames() const
    {
        return roles;
    }
     
    void ThreeDModelTexture::addTexture(Texture tex) const
    {
        ThreeDModelTexture::textures << tex;
    }
     
    int ThreeDModelTexture::rowCount(const QModelIndex &parent) const
    {
        return ThreeDModelTexture::textures.size();
    }
     
    int ThreeDModelTexture::Count()
    {
        return ThreeDModelTexture::textures.size();
    }
     
    int ThreeDModelTexture::boolMatchSearch(int id)
    {
        for(int i=0; i<ThreeDModelTexture::textures.size();i++)
        {
            if (ThreeDModelTexture::textures.at(i).getIDModel()==id)
            {
                return i;
            } else {
                return -1;
            }
        }
        return -1;
    }
     
    int idmodel(const int &id)
    {
        for(int i=0; i<ThreeDModelTexture::textures.size();i++)
        {
            if (ThreeDModelTexture::textures.at(i).getIDModel()==id)
            {
                return ThreeDModelTexture::textures.at(i).getIDModel();
            } else {
                return -1;
            }
        }
    }
    QString name(const int &id)
    {
        for(int i=0; i<ThreeDModelTexture::textures.size();i++)
        {
            if (ThreeDModelTexture::textures.at(i).getIDModel()==id)
            {
                return ThreeDModelTexture::textures.at(i).getName();
            } else {
                return "";
            }
        }
    }
    QString url(const int &id)
    {
        for(int i=0; i<ThreeDModelTexture::textures.size();i++)
        {
            if (ThreeDModelTexture::textures.at(i).getIDModel()==id)
            {
                return ThreeDModelTexture::textures.at(i).getUrl();
            } else {
                return "";
            }
        }
    }
    

    main.cpp

       ...
        ThreeDModels.addThreeDModel(new ThreeDModel("false", 1, "ccc","c"));
        ThreeDModels.addThreeDModel(new ThreeDModel("false", 3, "aaa","a"));
        ThreeDModels.addThreeDModel(new ThreeDModel("false", 5, "bbb","b"));
     
     
        TextureModel.addTexture(Texture(1, "masta", "masta.jpg"));
        TextureModel.addTexture(Texture(3, "aaa", "aaa.jpg"));
        TextureModel.addTexture(Texture(5, "bbb", "bbb.jpg"));
        TextureModel.addTexture(Texture(3, "ttt", "ttt.jpg"));
     
        view.engine()->rootContext()->setContextProperty("ThreeDM", &ThreeDModels);
        view.engine()->rootContext()->setContextProperty("TextureM", &TextureModel);
     
        view.setSource(QUrl("qrc:/Samples/Analysis/ViewshedGeoElement/ViewshedGeoElement.qml"));
        ...
    

    ViewshedGeoElement.qml

    /C1.TableView {
                anchors.top: rowLayout.bottom
                anchors.left: parent.left
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                id: tableView
                clip: true
                sortIndicatorVisible: true
                currentRow: rowCount ? 0 : -1
                model: SortFilterProxyModel {
                    source: ThreeDM.rowCount() > 0 ? ThreeDM : null
     
                    sortOrder: tableView.sortIndicatorOrder
                    sortCaseSensitivity: Qt.CaseInsensitive
                    sortRole: ThreeDM.rowCount() && tableView.getColumn(tableView.sortIndicatorColumn).role !== "check" > 0 ?
                                  tableView.getColumn(tableView.sortIndicatorColumn).role : ""
                }
                C1.TableViewColumn {
                    width: 30
                    role: "check"
                    resizable: false
                    delegate: C1.CheckBox {
                        id: checkBox
                        anchors.left: parent.left
                        anchors.leftMargin: parent.width / 3
                        checked: model.check
                        onVisibleChanged: if (visible) checked = model.check
                        onClicked: model.check = checked
                    }
                }
                C1.TableViewColumn {
                    width: 30
                    role: "id"
                    title: "id"
                }
                C1.TableViewColumn {
                    width: 90
                    role: "description"
                    title: "description"
                }
                C1.TableViewColumn {
                    width: 60
                    role: "code"
                    title: "code"
                }
                C1.TableViewColumn {
                    width: 100
                    role: "combobox"
                    resizable: false
                    title: "Текстура"
                    delegate: C1.ComboBox {
                        id: comboListTexture
                        model: ListModel {
                            id: comboModel
                            dynamicRoles: true
                        }
                        Component.onCompleted: {
                            reload()
                        }
                        textRole:"name"
                        onActivated: console.log("Combo Box Index Changed To:", index, model.data(model.index(index, 0), Qt.UserRole + 2 ))
     
                        function reload() {
                            var i = comboListTexture.currentIndex
                            // console.log("TextureM.boolMatchSearch(1)",TextureM.getidmodel(1),TextureM.getname(1),TextureM.geturl(1))
                            // console.log("TextureM.boolMatchSearch(1)",TextureM._name,TextureM._idModel,TextureM._url)
     
                            comboListTexture.model = TextureM
                            comboListTexture.currentIndex = i
                        }
                    }
                }
     
    ...
                style: TableViewStyle {
                    headerDelegate: Rectangle {
                        height: textItem.implicitHeight
                        width: textItem.implicitWidth
                        Text {
                            id: textItem
                            anchors.fill: parent
                            anchors.leftMargin: 6
                            verticalAlignment: Text.AlignVCenter
                            horizontalAlignment: styleData.textAlignment
                            text: styleData.value
                        }
                        C1.CheckBox {
                            anchors.left: parent.left
                            anchors.leftMargin: parent.width / 3
                            property bool isPressed: styleData.pressed
                            visible: styleData.column === 0
                            onIsPressedChanged: {
                                if (isPressed && styleData.column === 0) {
                                    checked  = !checked
                                    for(var i = 0; i < ThreeDM.rowCount(); i++) {
                                        ThreeDM.setData(ThreeDM.index(i, 0), checked, "check")
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.