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. Add map items at run-time
Forum Updated to NodeBB v4.3 + New Features

Add map items at run-time

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 241 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    I'm trying to automate a bit of logic, instead of hard-write values each time.
    My application may load QML pages into a QQuickWidget. Every page is different but has the same structure. There are some Text controls that have a unique name. They must show a value retrieved by the C++ backend class.

    First thoughts. I defined a QVariantMap where the keys are the items' names. Once the map is filled by my C++ class the QML page can show the actual values. Something like this:

    C++

    public:
        Q_PROPERTY(QVariantMap data READ data NOTIFY dataChanged)
        QVariantMap data() const { return _mapData; }
    
    private:
        QVariantMap _mapData;
    
    signals:
        void dataChanged();
    
    // ...
    
        QQmlContext *rootContext = ui->quickWidget->rootContext();
        rootContext->setContextProperty("App", this);
        ui->quickWidget->setSource(QUrl(filename));
    
        _mapData.insert("123", "Hello");
        _mapData.insert("124", "Hello");
        emit dataChanged();
    

    QML

    Text {
        id: txt123
        text: App.data["123"]
    }
    
    Text {
        id: txt124
        text: App.data["123"]
    }
    

    Now the problem is I have different keys for each QML page. I might define a C++ slot so the QML page can tell what keys it has:

    signal qmlSignal(string msg)
    
    function foo()
    {
        qmlSignal("123,124,125")
    }
    

    and in C++ I can split the string and insert them as map keys.
    It's ok but still I need to manually compile the list.

    I'm looking for an automagic way, like what the QMap::operator[] does. I mean, the first time the QML object try to read a value with a given key, if this is not present it should add to the mapData. Hence, every time I refresh the values I can find new keys.

    In C++ I know how to do this (using the [] operator as said) but how to do the same in QML using only the App.data[key] call?
    Unfortunately the getter function doesn't allow me to retrieve the current key, otherwise it would be easy enough to check whether it exists.

    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