Important: Please read the Qt Code of Conduct -

QList<QObject*> model question

  • 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 (
            qDebug() << "DB connection opened.";
            QSqlQuery leaderFetch;
            //Fetch data to be filled in  leaderboardData model
            QString leader_query = QString("SELECT ,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 ( {
                    leaderList.append(new LeaderboardData(leaderFetch.value(0).toInt(), leaderFetch.value(1).toString(),leaderFetch.value(2).toInt()));
                     qDebug() << "Querry  got user: "<<leaderFetch.value(1).toString();
                qDebug() << "Error happened - " << l_db.lastError().text();
                qDebug() << "Closing connection";
                gotError("Ooops, there seems to be a problem");
        QQuickView view;
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("leaderboardModel", QVariant::fromValue(leaderList));

    and I call it in QML as

            TableView {
                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?

  • Moderators

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

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


    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?

  • Moderators

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

  • @raven-worx Thanks, will try that.

Log in to reply