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. Display and refresh a dynamic list on QML
Forum Updated to NodeBB v4.3 + New Features

Display and refresh a dynamic list on QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 6 Posters 3.8k 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.
  • J Offline
    J Offline
    jiji
    wrote on last edited by
    #1

    Hi all,
    In my application I need to create a dynamic list, I retrieved my data from json and I used QML for views.
    I declare the list in main using setContextProperty as follows:
    int main(int argc, char argv[])
    {
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    qputenv("QT_QUICK_CONTROLS_STYLE", "material");
    QGuiApplication app(argc, argv);
    ApplicationUI appui;
    QQmlApplicationEngine engine;
    QQmlContext context = engine.rootContext();
    context->setContextProperty("myApp", &appui);

    //Users list
    QList<User*> listuser;
    listuser=u.getallUser() ;
    UserEntityModel modeluser;
    for(int i =0;i<listuser.size();i++ )
    {
    QString fullname = listuser[i]->fullname();
    QString email= listuser[i]->Email();
    QString grade= listuser[i]->grade();
    QString evaluation = listuser[i]->evaluation();
    QString photo=listuser[i]->photo();
    modeluser.addUserEntity(UserEntity(fullname,email,grade,evaluation,photo));
    }
    context->setContextProperty("userModel", &modeluser);
    engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
    return app.exec();
    }
    In QML I retrieve my list as follows:
    ListView {
    id: listViewuser
    anchors.bottomMargin: -3
    anchors.leftMargin: -18
    anchors.topMargin: -3
    anchors.rightMargin: 5
    focus: true
    clip: true
    anchors.fill: parent
    model:userModel
    delegate: userComponent
    section.property: "sortGroup"
    section.criteria: ViewSection.FullString
    section.delegate: stateChanged
    }
    But when I add a new object, I need to close the application and start the new main so that the list will be refreshed in the graphical interface (QML).
    While I want to refreshethe list automatically without closing the application.
    I try to declare the list in a different class than the main, but in QML I got this error: "userModel undefined" .

    E C 2 Replies Last reply
    0
    • J jiji

      Hi all,
      In my application I need to create a dynamic list, I retrieved my data from json and I used QML for views.
      I declare the list in main using setContextProperty as follows:
      int main(int argc, char argv[])
      {
      QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      qputenv("QT_QUICK_CONTROLS_STYLE", "material");
      QGuiApplication app(argc, argv);
      ApplicationUI appui;
      QQmlApplicationEngine engine;
      QQmlContext context = engine.rootContext();
      context->setContextProperty("myApp", &appui);

      //Users list
      QList<User*> listuser;
      listuser=u.getallUser() ;
      UserEntityModel modeluser;
      for(int i =0;i<listuser.size();i++ )
      {
      QString fullname = listuser[i]->fullname();
      QString email= listuser[i]->Email();
      QString grade= listuser[i]->grade();
      QString evaluation = listuser[i]->evaluation();
      QString photo=listuser[i]->photo();
      modeluser.addUserEntity(UserEntity(fullname,email,grade,evaluation,photo));
      }
      context->setContextProperty("userModel", &modeluser);
      engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
      return app.exec();
      }
      In QML I retrieve my list as follows:
      ListView {
      id: listViewuser
      anchors.bottomMargin: -3
      anchors.leftMargin: -18
      anchors.topMargin: -3
      anchors.rightMargin: 5
      focus: true
      clip: true
      anchors.fill: parent
      model:userModel
      delegate: userComponent
      section.property: "sortGroup"
      section.criteria: ViewSection.FullString
      section.delegate: stateChanged
      }
      But when I add a new object, I need to close the application and start the new main so that the list will be refreshed in the graphical interface (QML).
      While I want to refreshethe list automatically without closing the application.
      I try to declare the list in a different class than the main, but in QML I got this error: "userModel undefined" .

      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @jiji What is this UserEntityModel?

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fheanor
        wrote on last edited by Fheanor
        #3

        Hello,
        Could you use the code syntax ?

        Something like :

        Rectangle {
            color : "red
        }
        
        E 1 Reply Last reply
        0
        • F Fheanor

          Hello,
          Could you use the code syntax ?

          Something like :

          Rectangle {
              color : "red
          }
          
          E Offline
          E Offline
          Eeli K
          wrote on last edited by
          #4

          @Fheanor Balise?

          1 Reply Last reply
          0
          • J jiji

            Hi all,
            In my application I need to create a dynamic list, I retrieved my data from json and I used QML for views.
            I declare the list in main using setContextProperty as follows:
            int main(int argc, char argv[])
            {
            QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            qputenv("QT_QUICK_CONTROLS_STYLE", "material");
            QGuiApplication app(argc, argv);
            ApplicationUI appui;
            QQmlApplicationEngine engine;
            QQmlContext context = engine.rootContext();
            context->setContextProperty("myApp", &appui);

            //Users list
            QList<User*> listuser;
            listuser=u.getallUser() ;
            UserEntityModel modeluser;
            for(int i =0;i<listuser.size();i++ )
            {
            QString fullname = listuser[i]->fullname();
            QString email= listuser[i]->Email();
            QString grade= listuser[i]->grade();
            QString evaluation = listuser[i]->evaluation();
            QString photo=listuser[i]->photo();
            modeluser.addUserEntity(UserEntity(fullname,email,grade,evaluation,photo));
            }
            context->setContextProperty("userModel", &modeluser);
            engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
            return app.exec();
            }
            In QML I retrieve my list as follows:
            ListView {
            id: listViewuser
            anchors.bottomMargin: -3
            anchors.leftMargin: -18
            anchors.topMargin: -3
            anchors.rightMargin: 5
            focus: true
            clip: true
            anchors.fill: parent
            model:userModel
            delegate: userComponent
            section.property: "sortGroup"
            section.criteria: ViewSection.FullString
            section.delegate: stateChanged
            }
            But when I add a new object, I need to close the application and start the new main so that the list will be refreshed in the graphical interface (QML).
            While I want to refreshethe list automatically without closing the application.
            I try to declare the list in a different class than the main, but in QML I got this error: "userModel undefined" .

            C Offline
            C Offline
            c64zottel
            wrote on last edited by
            #5

            @jiji Do you inform the ListView that the data changed? You have to send a signal, for example the dataChanged signal. I assume, since your ListView shows data that you are already using on of Qt's models.

            You can look up dataChanged in my post here:
            https://forum.qt.io/topic/78802/datachange-signal-ignored-by-gridview/2

            And in general you can read how data can be set in a model here:
            http://doc.qt.io/qt-5/qabstractitemmodel.html#setData

            Although, you don't have to use setData, but you have to send the signal.

            1 Reply Last reply
            0
            • ibiaI Offline
              ibiaI Offline
              ibia
              wrote on last edited by
              #6

              Hello, dear developer! Please, use the "forceLayout()" method on your ListView. It automatically updates it in order to match the datas of the model.

              Hope it helped!

              C 1 Reply Last reply
              1
              • ibiaI ibia

                Hello, dear developer! Please, use the "forceLayout()" method on your ListView. It automatically updates it in order to match the datas of the model.

                Hope it helped!

                C Offline
                C Offline
                chimobien
                wrote on last edited by
                #7

                @ibia Hi. I did the way u said, but nothing change. so sad .ohoh

                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