Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved QList<QObject*> model question

    General and Desktop
    2
    5
    668
    Loading More Posts
    • 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.
    • Verhoher
      Verhoher last edited by

      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 Reply Quote 0
      • raven-worx
        raven-worx Moderators last edited by

        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

        Verhoher 1 Reply Last reply Reply Quote 0
        • Verhoher
          Verhoher @raven-worx last edited by

          @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-worx 1 Reply Last reply Reply Quote 0
          • raven-worx
            raven-worx Moderators @Verhoher last edited by 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.

            --- 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

            Verhoher 1 Reply Last reply Reply Quote 1
            • Verhoher
              Verhoher @raven-worx last edited by

              @raven-worx Thanks, will try that.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post