Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QList<QObject*> model question
Qt 6.11 is out! See what's new in the release blog

QList<QObject*> model question

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.7k Views
  • 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.
  • VerhoherV Offline
    VerhoherV Offline
    Verhoher
    wrote on last edited by
    #1

    Hello, I'm trying to build an leader-board model that would be filled with Object List, the code looks like:

    void UserHandler::handleLeaderboard()
    {
        qDebug() << "In UserHandler.handleLeaderboard";
        QList<QObject*> leaderList;
    
        if (l_db.open())
        {
            qDebug() << "DB connection opened.";
            QSqlQuery leaderFetch;
            //Fetch data to be filled in  leaderboardData model
            QString leader_query = QString("SELECT rowus.place ,us.login,us.Points from users us,(SELECT ROW_NUMBER() OVER (ORDER BY points desc) AS place FROM users) rowus where rowus.Place < 101 order by Points desc, login asc;");
            qDebug() << "Querry "<< leader_query;
            if (leaderFetch.exec(leader_query))
            {
                while (leaderFetch.next()) {
    
                    leaderList.append(new LeaderboardData(leaderFetch.value(0).toInt(), leaderFetch.value(1).toString(),leaderFetch.value(2).toInt()));
                     qDebug() << "Querry  got user: "<<leaderFetch.value(1).toString();
                }
                l_db.close();
            }
            else
            {
                qDebug() << "Error happened - " << l_db.lastError().text();
                qDebug() << "Closing connection";
                l_db.close();
                gotError("Ooops, there seems to be a problem");
            }
        }
        QQuickView view;
        view.setResizeMode(QQuickView::SizeRootObjectToView);
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("leaderboardModel", QVariant::fromValue(leaderList));
    }
    

    and I call it in QML as

            TableView {
                id:tblLeaders
                anchors.fill: parent
                TableViewColumn {
                    id: colView
                    role: "place"
                    title: "Place"
                    width: 36
                }
                TableViewColumn {
                    id: colLogin
                    role: "login"
                    title: "Username"
                    width: parent.width - colView.width - colPoints.width
                }
                TableViewColumn {
                    id: colPoints
                    role: "points"
                    title: "Points"
                    width: 128
                }
                model: leaderboardModel
            }
    

    But When the view gets rendered I get:

    qrc:/Leaderboards.qml:56: ReferenceError: leaderboardModel is not defined
    

    Where and how i should define the context for this model for it to be accessible in QML?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      make sure setContextProperty() call happens before you set the QML file.
      Is this the case?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      VerhoherV 1 Reply Last reply
      0
      • raven-worxR raven-worx

        make sure setContextProperty() call happens before you set the QML file.
        Is this the case?

        VerhoherV Offline
        VerhoherV Offline
        Verhoher
        wrote on last edited by
        #3

        @raven-worx Oh, this might be the case. I have a TabView (that is not visible until the handleLeaderboard logic is executed) storing the Leaderboards.qml. But I guess Leaderboards.qml was set on declaring it with

        visible:false
        

        and the error was shown only on switching to the tab containing the QML. Is it so?

        Also what could I use to not set the QML file until a custom specific signal is emitted?

        raven-worxR 1 Reply Last reply
        0
        • VerhoherV Verhoher

          @raven-worx Oh, this might be the case. I have a TabView (that is not visible until the handleLeaderboard logic is executed) storing the Leaderboards.qml. But I guess Leaderboards.qml was set on declaring it with

          visible:false
          

          and the error was shown only on switching to the tab containing the QML. Is it so?

          Also what could I use to not set the QML file until a custom specific signal is emitted?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Verhoher said:

          Also what could I use to not set the QML file until a custom specific signal is emitted?

          alternatively you could initially add a QObject* as context property. and let this object manage the list retrieval. Then from the object you can trigger a signal and react on it in QML for example.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          VerhoherV 1 Reply Last reply
          1
          • raven-worxR raven-worx

            @Verhoher said:

            Also what could I use to not set the QML file until a custom specific signal is emitted?

            alternatively you could initially add a QObject* as context property. and let this object manage the list retrieval. Then from the object you can trigger a signal and react on it in QML for example.

            VerhoherV Offline
            VerhoherV Offline
            Verhoher
            wrote on last edited by
            #5

            @raven-worx Thanks, will try that.

            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