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. C++ updating ListView
Forum Updated to NodeBB v4.3 + New Features

C++ updating ListView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 3 Posters 3.5k 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.
  • G Offline
    G Offline
    Giba
    wrote on last edited by
    #1

    Hi,
    I need to know how can I change the MainForm.ui.qml objects using C++.
    In this case I have a access in a database and I want to add items in my ListView using C++.

    MainForm.ui.qml
    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.2

    Item {
    id: item1
    width: 640
    height: 480

    property alias buttonD: buttonDespensa
    property alias buttonL: buttonLista
    property alias buttonP: buttonProdutos
    .....
    .....
    .....
    ListView {
        id: listView1
        width: 110
        height: 160
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: parent.top
        anchors.topMargin: 35
        delegate: Item {
            x: 5
            width: 80
            height: 40
            Row {
                id: row1
                Rectangle {
                    width: 40
                    height: 40
                    color: colorCode
                }
    
                Text {
                    text: name
                    font.bold: true
                    anchors.verticalCenter: parent.verticalCenter
                }
                spacing: 10
            }
        }
        model: ListModel {
            ListElement {
                name: "Grey"
                colorCode: "grey"
            }
    
            ListElement {
                name: "Red"
                colorCode: "red"
            }
    
            ListElement {
                name: "Blue"
                colorCode: "blue"
            }
    
            ListElement {
                name: "Green"
                colorCode: "green"
            }
        }
    }
    

    }

    main.cpp
    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include "qdebug.h"
    #include "database.h"
    #include <QtQuick>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    DataBase banco;
    
    return app.exec();
    

    }

    database.cpp
    #include "database.h"
    #include <QtSql>
    #include "qdebug.h"

    DataBase::DataBase()
    {
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("despensa.sqlite");
    if (db.open()) {
    QSqlQuery query("SELECT * FROM produtos;");
    while (query.next()){
    // listView1.item << query.value(1).toString();
    qDebug() << query.value(1).toString();
    }
    db.close();
    }
    }

    I need add items (ListElement) using query.value(1).toString() from C++. How can I do that?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi, and welcome to the Qt forum! Instead of creating the model with QtQuick you could create it in C++. The documentation has example code for this, see: Using C++ Models with Qt Quick Views.

      G 1 Reply Last reply
      0
      • ? A Former User

        Hi, and welcome to the Qt forum! Instead of creating the model with QtQuick you could create it in C++. The documentation has example code for this, see: Using C++ Models with Qt Quick Views.

        G Offline
        G Offline
        Giba
        wrote on last edited by
        #3

        @Wieland
        I read this document and I tried this code:
        main.cpp
        QStringList dataList;
        dataList.append("item 1");
        dataList.append("item 2");
        dataList.append("item 3");

        QQuickView view;
        QQmlContext *ctxt = view.rootContext();
        ctxt->setContextProperty("myModel",QVariant::fromValue(dataList));
        
        view.setSource(QUrl("qrc:/MainForm.ui.qml"));
        view.show();
        

        MainForm.ui.qml
        ...
        ListView {
        id: listView2
        width: 110;
        height: 160
        anchors.left: parent.left
        anchors.leftMargin: 10
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: parent.top
        anchors.topMargin: 35

            model: myModel
            delegate: Rectangle {
                height: 25
                width: 100
                Text { text: modelData }
            }
        }
        

        ...

        That code works, but it broke my layout.
        In the design mode, my layout is perfect.

        CharbyC 1 Reply Last reply
        0
        • G Giba

          @Wieland
          I read this document and I tried this code:
          main.cpp
          QStringList dataList;
          dataList.append("item 1");
          dataList.append("item 2");
          dataList.append("item 3");

          QQuickView view;
          QQmlContext *ctxt = view.rootContext();
          ctxt->setContextProperty("myModel",QVariant::fromValue(dataList));
          
          view.setSource(QUrl("qrc:/MainForm.ui.qml"));
          view.show();
          

          MainForm.ui.qml
          ...
          ListView {
          id: listView2
          width: 110;
          height: 160
          anchors.left: parent.left
          anchors.leftMargin: 10
          anchors.horizontalCenter: parent.horizontalCenter
          anchors.top: parent.top
          anchors.topMargin: 35

              model: myModel
              delegate: Rectangle {
                  height: 25
                  width: 100
                  Text { text: modelData }
              }
          }
          

          ...

          That code works, but it broke my layout.
          In the design mode, my layout is perfect.

          CharbyC Offline
          CharbyC Offline
          Charby
          wrote on last edited by Charby
          #4

          @Giba I think there are several options to achieve your goal :
          1 - you can have you model designed in the C++ side as @Wieland proposed
          2 - give a name to your QML model, so that you can retrieve it from C++ (despite this will works and it is - unfortunately - well documented, it is a very bad option as it will break the QtQuick design principle to keep the C++ agnostic of the QML)
          3 - you can have a kind of controler object in C++ that would be passed to the QML engine to drive the model using signals connection (I mean you would - in the QML - react on specific signal such as removeRow(id) to handle the removal of a row in the model...)

          G 1 Reply Last reply
          0
          • CharbyC Charby

            @Giba I think there are several options to achieve your goal :
            1 - you can have you model designed in the C++ side as @Wieland proposed
            2 - give a name to your QML model, so that you can retrieve it from C++ (despite this will works and it is - unfortunately - well documented, it is a very bad option as it will break the QtQuick design principle to keep the C++ agnostic of the QML)
            3 - you can have a kind of controler object in C++ that would be passed to the QML engine to drive the model using signals connection (I mean you would - in the QML - react on specific signal such as removeRow(id) to handle the removal of a row in the model...)

            G Offline
            G Offline
            Giba
            wrote on last edited by
            #5

            @Charby
            I want just manipulate the ui objects using C++. I don't want create ui objects using C++.
            The most exemples that I can see in internet is creating and using object from C++.
            I can't understand the difference in MainForm.ui.qml and main.qml file.
            My C++ starts only main.qml (engine) but the layout uses the both files.

            CharbyC 1 Reply Last reply
            0
            • G Giba

              @Charby
              I want just manipulate the ui objects using C++. I don't want create ui objects using C++.
              The most exemples that I can see in internet is creating and using object from C++.
              I can't understand the difference in MainForm.ui.qml and main.qml file.
              My C++ starts only main.qml (engine) but the layout uses the both files.

              CharbyC Offline
              CharbyC Offline
              Charby
              wrote on last edited by
              #6

              @Giba Manipulating qml objects from c++ (this can be done by iterating every object from the root element of the engine finding a specific name) should be avoided as it won't be working if you change the name of QML element.
              if you really want to : you can find information about how to acheive this here

              The ui.qml is generated by the UI designer.

              G 1 Reply Last reply
              0
              • CharbyC Charby

                @Giba Manipulating qml objects from c++ (this can be done by iterating every object from the root element of the engine finding a specific name) should be avoided as it won't be working if you change the name of QML element.
                if you really want to : you can find information about how to acheive this here

                The ui.qml is generated by the UI designer.

                G Offline
                G Offline
                Giba
                wrote on last edited by
                #7

                @Charby About manipulating qml object thank you. I thought it was move easy like create a class with same qml object name, but it isn't.

                My question about MainForm.ui.qml and main.qml is how theses files works?
                My application is designed in MainForm.ui.qml, but I have some elements in mail.qml (like menuBar) that can't use in ui, but there is in my application. The source code in C++ call only main.qml, where is the MainForm.ui.qml file call?

                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