Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Accessing QMap items in QML
Forum Updated to NodeBB v4.3 + New Features

Accessing QMap items in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 186 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SteMMoS Offline
    SteMMoS Offline
    SteMMo
    wrote on last edited by SteMMo
    #1

    Hi all,
    I have a number of settings stored by a QMap( name, value).

    class MachineSettings : public QAbstractListModel
    {
       Q_OBJECT
    
    public:
       MachineSettings();
    
       void insert(QString &key, QString &value);
       Q_INVOKABLE QString get(QString key){ return _settings[key]; };
    
      int rowCount(const QModelIndex & parent) const
      {
        Q_UNUSED(parent);
        qDebug() << "[rowCount] return: " << _settings.count();
        return _settings.count();
      };
    
       QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
       {
        qDebug() << "[data] " << index.row() << " - " << role;
        return QVariant("**");
       };
    
    private:
       QMap<QString, QString> _settings;
    };
    

    I passed the variable to QML and used as a ListView model.
    How can I access to the QMap's items?

        ListView {
            id: listView
            anchors.left: parent.left
           ...
            interactive: false
            model: mysettings
            delegate: Item {
                x: 5
                width: 120
                height: 40
                Row {
                    id: row1
                    Rectangle {
                        width: 40
                        height: 40
                        color: "#ffaaaa"
                    }
    
                    Text {
                        text: "Key"
                        anchors.verticalCenter: parent.verticalCenter
                        font.bold: true
                    }
                    Text {
                        text: "Val "+model.email
                        anchors.verticalCenter: parent.verticalCenter
                        font.bold: true
                    }
                    spacing: 10
                }
            }
        }
    

    It generates a list of some rows (how to handle the scroll bar??) containing text 'Key Val undefined'.
    The debug output in the rowCount() returns '79' but the qDebugs in data() function does not appair in the console (?).

    Thanks

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved