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. Binding list from C++ to QML
Forum Updated to NodeBB v4.3 + New Features

Binding list from C++ to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qml binding
25 Posts 3 Posters 13.9k 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.
  • T Offline
    T Offline
    TuanLT
    wrote on 30 Oct 2015, 02:47 last edited by
    #16

    Thanks you very much!

    But, now i have new problem, when i using signal and slot to communicate between cpp file and qml file. I'm using code below:

    Cpp file (main.cpp)

    QQmlApplicationEngine engine;
    QQmlContext* context = engine.rootContext();
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    QObject *topLevel = engine.rootObjects().value(0);
    QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
    KeywordBusiness business;
    QObject::connect(window,SIGNAL(savekeywords(KeywordsModel)), &business, SLOT(SaveChangedData(KeywordsModel)));

    on class KeywordBusiness .h I define a slot SaveChangedData with parameter is KeywordsModel extends QObject

    On qml file. I define a signal savekeywords with also parameter is KeywordsModel.

    But when i click button to call signal savekeywords , in cpp file the QObject::connect funtion not working.

    Please help me, I was missing something?

    Thanks!

    T 1 Reply Last reply 30 Oct 2015, 17:38
    0
    • T TuanLT
      30 Oct 2015, 02:47

      Thanks you very much!

      But, now i have new problem, when i using signal and slot to communicate between cpp file and qml file. I'm using code below:

      Cpp file (main.cpp)

      QQmlApplicationEngine engine;
      QQmlContext* context = engine.rootContext();
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      QObject *topLevel = engine.rootObjects().value(0);
      QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
      KeywordBusiness business;
      QObject::connect(window,SIGNAL(savekeywords(KeywordsModel)), &business, SLOT(SaveChangedData(KeywordsModel)));

      on class KeywordBusiness .h I define a slot SaveChangedData with parameter is KeywordsModel extends QObject

      On qml file. I define a signal savekeywords with also parameter is KeywordsModel.

      But when i click button to call signal savekeywords , in cpp file the QObject::connect funtion not working.

      Please help me, I was missing something?

      Thanks!

      T Offline
      T Offline
      TonyN
      wrote on 30 Oct 2015, 17:38 last edited by
      #17

      @TuanLT Dude, I post it for someone to help me to solve my problem. Please post your question in the separate post.

      1 Reply Last reply
      0
      • T TonyN
        29 Oct 2015, 22:16

        I simplified the MainForm.ui.qml (as below) and find that the QML report "TypeError: Cannot read property 'items' of undefined" for each instance of the the page when I execute the app. According to this, I think I have to register Items_model. However I am unable to do so with qmlRegisterType()

        (I rename the property Items in Page_model to have lowercamel: Q_PROPERTY(Items_model* items READ Items WRITE setItems NOTIFY itemsChanged) )

        import QtQuick 2.4
        import QtQuick.Controls 1.3
        
        Rectangle {
            id: rectangleMain
            property alias quitButton: quitButton
            property alias pagesView: pagesView
        
            width: 360
            height: 360
        
        
        
        
            ListView {
                id: pagesView
                width: 500
                orientation: ListView.Horizontal
                spacing: 2
        
                anchors {
                    bottom: quitButton.top
                    bottomMargin: 8
                    right: parent.right
                    rightMargin: 0
                    left: parent.left
                    leftMargin: 0
                    top: parent.top
                    topMargin: 0
                }
        
                delegate: Rectangle {
                    id:paView
                    height: parent.height
                    width:100
                    color: "transparent"
                    border.color: "teal"
                    border.width: 1
                    radius: 6
        
                    GridView {
        
                        anchors { rightMargin: 6
                            leftMargin: 6
                            bottomMargin: 6
                            topMargin: 6
                            fill: parent
                        }
                        cellWidth: width / 2
                        cellHeight: 50
        
                        model:model.items
        
                        delegate: Rectangle {
                            height: 46
                            width: 46
                            color: "blue" //colorCode
                            Text {
        
                                anchors.horizontalCenter: parent.horizontalCenter
                                anchors.verticalCenter: parent.verticalCenter
                                text:model.displayText
                            }
                        }
                    }
                }
            }
        
            Button {
                id: quitButton
                y: 325
                text: qsTr("Quit")
                anchors {
                    right: parent.right
                    rightMargin: 50
                    left: parent.left
                    leftMargin: 50
                    bottom: parent.bottom
                    bottomMargin: 8
                }
            }
        }
        
        P Offline
        P Offline
        p3c0
        Moderators
        wrote on 31 Oct 2015, 11:22 last edited by
        #18

        @TonyN

        I simplified the MainForm.ui.qml (as below) and find that the QML report "TypeError: Cannot read property 'items' of undefined" for each instance of the the page when I execute the app.

        Since that property is in parent model try accessing it with its parent object.
        model: pagesView.model.items

        157

        T 1 Reply Last reply 2 Nov 2015, 16:04
        0
        • P p3c0
          31 Oct 2015, 11:22

          @TonyN

          I simplified the MainForm.ui.qml (as below) and find that the QML report "TypeError: Cannot read property 'items' of undefined" for each instance of the the page when I execute the app.

          Since that property is in parent model try accessing it with its parent object.
          model: pagesView.model.items

          T Offline
          T Offline
          TonyN
          wrote on 2 Nov 2015, 16:04 last edited by
          #19

          @p3c0 : Thanks, you are correct about that, but that is not enough to solve the problem. I find out what was missing. Instead, create the Q_PROPERTY, I have to create custom "roles" like these (example code below) in the model. Again, I don't know this is the right way or not, but it solved my problem of binding models further down on the chain (second level). But then now, I find out that the QAbstractTableModel may not the right choice :~) Despite I implemented columnCount(), it only ask for data in rows of column 0!!

          QHash<int, QByteArray> Pages_model::roleNames() const
          {
              QHash<int, QByteArray> roles;
              roles[DisplayNameRole] = "displayName";
              roles[DisplayItemsRole] = "displayItems";
          
              return roles;
          }
          
          QVariant Pages_model::data(const QModelIndex &index, int role) const
          {
              if (!index.isValid())
                  return QVariant();
          
              int i = index.row();
              if (i < 0 || i >= _pagesModelList.count())
                  return QVariant();
          
              const Page_model *pPage = _pagesModelList[i];
              if (role == DisplayNameRole)
              {
                  return pPage->displayName();
              }
              else if (role == DisplayItemsRole)
              {
                  return QVariant::fromValue(pPage->Items());
              }
          
              return QVariant();
          }
          
          P 1 Reply Last reply 3 Nov 2015, 09:53
          0
          • T TonyN
            2 Nov 2015, 16:04

            @p3c0 : Thanks, you are correct about that, but that is not enough to solve the problem. I find out what was missing. Instead, create the Q_PROPERTY, I have to create custom "roles" like these (example code below) in the model. Again, I don't know this is the right way or not, but it solved my problem of binding models further down on the chain (second level). But then now, I find out that the QAbstractTableModel may not the right choice :~) Despite I implemented columnCount(), it only ask for data in rows of column 0!!

            QHash<int, QByteArray> Pages_model::roleNames() const
            {
                QHash<int, QByteArray> roles;
                roles[DisplayNameRole] = "displayName";
                roles[DisplayItemsRole] = "displayItems";
            
                return roles;
            }
            
            QVariant Pages_model::data(const QModelIndex &index, int role) const
            {
                if (!index.isValid())
                    return QVariant();
            
                int i = index.row();
                if (i < 0 || i >= _pagesModelList.count())
                    return QVariant();
            
                const Page_model *pPage = _pagesModelList[i];
                if (role == DisplayNameRole)
                {
                    return pPage->displayName();
                }
                else if (role == DisplayItemsRole)
                {
                    return QVariant::fromValue(pPage->Items());
                }
            
                return QVariant();
            }
            
            P Offline
            P Offline
            p3c0
            Moderators
            wrote on 3 Nov 2015, 09:53 last edited by
            #20

            @TonyN

            But then now, I find out that the QAbstractTableModel may not the right choice :~) Despite I implemented columnCount(), it only ask for data in rows of column 0!!

            That's right GridView or ListView only deals with rows. In that case can't you replace it with QAbstractItemModel ? Keep everything other than columns.

            157

            T 2 Replies Last reply 3 Nov 2015, 15:49
            0
            • P p3c0
              3 Nov 2015, 09:53

              @TonyN

              But then now, I find out that the QAbstractTableModel may not the right choice :~) Despite I implemented columnCount(), it only ask for data in rows of column 0!!

              That's right GridView or ListView only deals with rows. In that case can't you replace it with QAbstractItemModel ? Keep everything other than columns.

              T Offline
              T Offline
              TonyN
              wrote on 3 Nov 2015, 15:49 last edited by
              #21

              @p3c0 Uh but then which view should I use? I read this,
              "QAbstractItemModel presents a hierarchy of tables, but the views currently provided by QML can only display list data."
              Does that mean, at the present time, nothing I can use except maybe create my own view!? If so, what is the best reading can you recommend?

              1 Reply Last reply
              0
              • P p3c0
                3 Nov 2015, 09:53

                @TonyN

                But then now, I find out that the QAbstractTableModel may not the right choice :~) Despite I implemented columnCount(), it only ask for data in rows of column 0!!

                That's right GridView or ListView only deals with rows. In that case can't you replace it with QAbstractItemModel ? Keep everything other than columns.

                T Offline
                T Offline
                TonyN
                wrote on 3 Nov 2015, 23:44 last edited by
                #22

                @p3c0 I solved the problem! Even without doing custom roles. I just simplified it, adding some qRegisterMetaType. The solution is not as I want to be, but it works. For now, I just have to figure out how to hide the dummy items. Thank you for pointing out some direction.

                P 1 Reply Last reply 4 Nov 2015, 05:51
                0
                • T TonyN
                  3 Nov 2015, 23:44

                  @p3c0 I solved the problem! Even without doing custom roles. I just simplified it, adding some qRegisterMetaType. The solution is not as I want to be, but it works. For now, I just have to figure out how to hide the dummy items. Thank you for pointing out some direction.

                  P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 4 Nov 2015, 05:51 last edited by p3c0 11 Apr 2015, 05:52
                  #23

                  @TonyN Congratulations :)
                  IMO you can use QAbstractItemModel with GridView(for grid like item positioning) or ListView (for list like item positioning). For hiding dummy items you can maintain a field for items and update it from the model whenever required.

                  157

                  T 1 Reply Last reply 4 Nov 2015, 14:25
                  0
                  • P p3c0
                    4 Nov 2015, 05:51

                    @TonyN Congratulations :)
                    IMO you can use QAbstractItemModel with GridView(for grid like item positioning) or ListView (for list like item positioning). For hiding dummy items you can maintain a field for items and update it from the model whenever required.

                    T Offline
                    T Offline
                    TonyN
                    wrote on 4 Nov 2015, 14:25 last edited by
                    #24

                    @p3c0 I certainly want to hear other opinion. I did attempt to use QAbstractItemModel, but then the GridView still treat model as a list!?
                    (my current solution is using QAbstractListModel with GridView, and compute item's index from its specified row and column, if there is no item at the requested index, I just return QVariant(). The rowCount() of QAbstractItemModel is the total item the can represent on the grid (row * column) )

                    P 1 Reply Last reply 5 Nov 2015, 05:45
                    0
                    • T TonyN
                      4 Nov 2015, 14:25

                      @p3c0 I certainly want to hear other opinion. I did attempt to use QAbstractItemModel, but then the GridView still treat model as a list!?
                      (my current solution is using QAbstractListModel with GridView, and compute item's index from its specified row and column, if there is no item at the requested index, I just return QVariant(). The rowCount() of QAbstractItemModel is the total item the can represent on the grid (row * column) )

                      P Offline
                      P Offline
                      p3c0
                      Moderators
                      wrote on 5 Nov 2015, 05:45 last edited by
                      #25

                      @TonyN GridView doesnot have the facility to get items using columns. AFAIK your current solution is the only way to go. Btw inside GridView you already have the index attached property.

                      157

                      1 Reply Last reply
                      0

                      25/25

                      5 Nov 2015, 05:45

                      • Login

                      • Login or register to search.
                      25 out of 25
                      • First post
                        25/25
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved