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. problem with get values from QHash in qml

problem with get values from QHash in qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 505 Views 2 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.
  • Q Offline
    Q Offline
    qAminzzz
    wrote on last edited by
    #1

    hi i want to get rows from a sqlite database and insert them into a listview
    i made a function which returns QList<QHash<QString, QVariant>>
    everything works fine but i can't get keys and values from the hash

    look at below codes

    my c++ code

    QList<QHash<QString, QVariant>> DataBase::getRows()
    {
        _db.open();
    
        if(_query.exec("select * from Persons"))
        {
            qInfo() << "Rows returned successfully";
    
            QList<QHash<QString, QVariant>> rows;
            QHash<QString, QVariant> columnsValue;
    
            while (_query.next())
            {
                columnsValue["id"]= _query.value("id");
                columnsValue["Name"] = _query.value("Name");
    
                rows.append(columnsValue);
            }
    
            _db.close();
            return rows;
        }
    
        else
        {
            qInfo() << "return rows failed";
            qInfo() << _query.lastError();
    
            _db.close();
        }
    
        _db.close();
    }
    

    my qml code:

        ListView
        {
            anchors.fill: parent
            model: ListModel
            {
                Component.onCompleted:
                {
                    var personsRows = DataBase.getRows()
    
                    for(var i = 0; i < personsRows.length; i++)
                    {
                        print(personsRows[i]["id"]);
                    }
                }
            }
    
            delegate: Label
            {
                text: index
            }
        }
    
    

    but it returns undefined

    thanks in advance

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      QML does not understand such types. You need to use QVariant, QVariantMap, QVariantHash etc.

      Or make a proper model with roles and then you can get data in QML via named roles roleNames().

      (Z(:^

      Q 1 Reply Last reply
      2
      • sierdzioS sierdzio

        QML does not understand such types. You need to use QVariant, QVariantMap, QVariantHash etc.

        Or make a proper model with roles and then you can get data in QML via named roles roleNames().

        Q Offline
        Q Offline
        qAminzzz
        wrote on last edited by
        #3

        @sierdzio thanks for answer, my problem solved thank you

        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