Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractTableModel + QML TableView: How to call setData?
Forum Updated to NodeBB v4.3 + New Features

QAbstractTableModel + QML TableView: How to call setData?

Scheduled Pinned Locked Moved Unsolved General and Desktop
40 Posts 3 Posters 10.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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by VRonin
    #4

    Starting from this example: https://wiki.qt.io/How_to_Use_a_Custom_Class_in_C%2B%2B_Model_and_QML_View

    Changing the delegate to something like:

    delegate: TextInput  {
                    text: edit.name
                    onEditingFinished: {
                        edit.name = text
                    }
                }
    

    is enough.
    Regarding how it works, see the very last paragraph of the wiki

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    B 1 Reply Last reply
    1
    • VRoninV VRonin

      Starting from this example: https://wiki.qt.io/How_to_Use_a_Custom_Class_in_C%2B%2B_Model_and_QML_View

      Changing the delegate to something like:

      delegate: TextInput  {
                      text: edit.name
                      onEditingFinished: {
                          edit.name = text
                      }
                  }
      

      is enough.
      Regarding how it works, see the very last paragraph of the wiki

      B Offline
      B Offline
      Bremenpl
      wrote on last edited by
      #5

      @VRonin Hi, Thanks for answer. I have no idea where does 'edit` id come from, even after reviewing your link...

      lprzenioslo.zut.edu.pl

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #6

        From https://wiki.qt.io/How_to_Use_a_Custom_Class_in_C%2B%2B_Model_and_QML_View

        accessing our data is very easy we just use the roleName.propertyName syntax. Since we saved our data in Qt::EditRole we used edit as role name (a list of the default role names is In QAbstractItemModel's documentation)

        basically edit is the name of the role as returned by QAbstractItemModel::roleNames(). The example doesn't overload it so the default name coresponding to Qt::EditRole is used

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        B 1 Reply Last reply
        0
        • VRoninV VRonin

          From https://wiki.qt.io/How_to_Use_a_Custom_Class_in_C%2B%2B_Model_and_QML_View

          accessing our data is very easy we just use the roleName.propertyName syntax. Since we saved our data in Qt::EditRole we used edit as role name (a list of the default role names is In QAbstractItemModel's documentation)

          basically edit is the name of the role as returned by QAbstractItemModel::roleNames(). The example doesn't overload it so the default name coresponding to Qt::EditRole is used

          B Offline
          B Offline
          Bremenpl
          wrote on last edited by Bremenpl
          #7

          @VRonin Thanks for clarification, this gets complicated. My override looks as follows:

          /**
           * @brief	Obtains the role names needed on the QML side.
           * @return	A hash of role names.
           */
          QHash<int, QByteArray> CVarTableModel::roleNames() const
          {
          	QHash<int, QByteArray> roles;
          	const int colCount = columnCount();
          
          	for (int col = 0; col < colCount; col++)
          		roles[Qt::UserRole + col] = role(col).toByteArray();
          
          	return roles;
          }
          

          And the role method is:

          /**
           * @brief	Obtains the role name string assiociated to the provided \p column
           * @param	column: Column number/ index.
           * @return	Role name string (as Qvariant) in case \p column is valid.
           */
          QVariant CVarTableModel::role(const int column) const
          {
          	if (!isColumnValid(column))
          		return QString();
          
          	return QVariant::fromValue(static_cast<Columns>(column));
          }
          

          Columns is a simple enum:

          	/**
          	 * @brief	An enumeration class providing the columns and the amount of
          	 *			columns as well (use ZCOUNT as always last member).
          	 */
          	enum class Columns
          	{
          		Name = 0,
          		Unit,
          		Value,
          
          		ZCOUNT,
          	};
          	Q_ENUM(Columns)
          

          So my role names would be Name, Unit and Value. How would this be consistent with the edit way if its custom?
          I would appreciate further help.

          lprzenioslo.zut.edu.pl

          VRoninV 1 Reply Last reply
          0
          • B Bremenpl

            @VRonin Thanks for clarification, this gets complicated. My override looks as follows:

            /**
             * @brief	Obtains the role names needed on the QML side.
             * @return	A hash of role names.
             */
            QHash<int, QByteArray> CVarTableModel::roleNames() const
            {
            	QHash<int, QByteArray> roles;
            	const int colCount = columnCount();
            
            	for (int col = 0; col < colCount; col++)
            		roles[Qt::UserRole + col] = role(col).toByteArray();
            
            	return roles;
            }
            

            And the role method is:

            /**
             * @brief	Obtains the role name string assiociated to the provided \p column
             * @param	column: Column number/ index.
             * @return	Role name string (as Qvariant) in case \p column is valid.
             */
            QVariant CVarTableModel::role(const int column) const
            {
            	if (!isColumnValid(column))
            		return QString();
            
            	return QVariant::fromValue(static_cast<Columns>(column));
            }
            

            Columns is a simple enum:

            	/**
            	 * @brief	An enumeration class providing the columns and the amount of
            	 *			columns as well (use ZCOUNT as always last member).
            	 */
            	enum class Columns
            	{
            		Name = 0,
            		Unit,
            		Value,
            
            		ZCOUNT,
            	};
            	Q_ENUM(Columns)
            

            So my role names would be Name, Unit and Value. How would this be consistent with the edit way if its custom?
            I would appreciate further help.

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #8

            So my role names would be Name, Unit and Value. How would this be consistent with the edit way if its custom?

            easy, replace edit with Name, Unit or Value. For example, if the Name role will contain a QString you can use:

            delegate: TextInput  {
                            text: Name
                            onEditingFinished: {
                                Name = text
                            }
                        }
            

            My override looks as follows:

            This is a very convoluted way to go about it. You can just use QMetaEnum::fromType<Derived::Columns>().valueToKeys(col);

            for (int col = 0; col < colCount; col++)

            Looks like columns and roles are the same thing here but since 1 year ago they are now 2 distinct things (as they are in QtWidgets): https://blog.qt.io/blog/2018/08/29/tableview/

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            B 1 Reply Last reply
            0
            • VRoninV VRonin

              So my role names would be Name, Unit and Value. How would this be consistent with the edit way if its custom?

              easy, replace edit with Name, Unit or Value. For example, if the Name role will contain a QString you can use:

              delegate: TextInput  {
                              text: Name
                              onEditingFinished: {
                                  Name = text
                              }
                          }
              

              My override looks as follows:

              This is a very convoluted way to go about it. You can just use QMetaEnum::fromType<Derived::Columns>().valueToKeys(col);

              for (int col = 0; col < colCount; col++)

              Looks like columns and roles are the same thing here but since 1 year ago they are now 2 distinct things (as they are in QtWidgets): https://blog.qt.io/blog/2018/08/29/tableview/

              B Offline
              B Offline
              Bremenpl
              wrote on last edited by
              #9

              @VRonin Thank you for follow-up. As for the method optimization, I will get back to that. But for the main problem, I did:

              Component
              {
              	id: theInputComp;
              
              	TextInput
              	{
              		id: textInputId;
              		anchors.fill: parent;
              
              		onEditingFinished:
              		{
              			theLoader.visible = false;
              			theCellText.visible = true;
              			//tableModel.setData(styleData.row,
              							   //styleData.column, text);
              			Name = text;
              		}
              	}
              }
              

              And received: qrc:/UBasicTable.qml:109: Error: Invalid write to global property "Name"
              Am I missing something?

              lprzenioslo.zut.edu.pl

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #10

                Name might be a reserved word. Try changing Name = 0, to colName = 0, and use text: colName and colName = text

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                B 1 Reply Last reply
                0
                • VRoninV VRonin

                  Name might be a reserved word. Try changing Name = 0, to colName = 0, and use text: colName and colName = text

                  B Offline
                  B Offline
                  Bremenpl
                  wrote on last edited by
                  #11

                  @VRonin Doesnt matter how I call it:

                  qrc:/UBasicTable.qml:109: Error: Invalid write to global property "colName"
                  qrc:/UBasicTable.qml:109: Error: Invalid write to global property "colName1" // this doesnt exist
                  

                  Any other hints :/?

                  lprzenioslo.zut.edu.pl

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Bremenpl
                    wrote on last edited by
                    #12

                    Just BTW... Figured out why doesnt the dataChanged signal fire the table update. I was calling it like this:

                    emit dataChanged(index, index, {role});
                    

                    and role was always Qt::EditRole. Now I call it without roles parameter:

                    emit dataChanged(index, index);
                    

                    And the TableView cells are being updated! Thanks for the help @VRonin, it led me to the right place for this one.
                    I still havent figured out how to automatically call setData from the QML though (using the presented workaround). If you have any more ideas please let me know.

                    lprzenioslo.zut.edu.pl

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #13

                      Are you sure you are inside delegate:?
                      This is a minimal working example:

                      #include <QGuiApplication>
                      #include <QQmlContext>
                      #include <QQmlApplicationEngine>
                      #include <QStringListModel>
                      #include <QDebug>
                      class MyModel : public QStringListModel{
                      public:
                          MyModel(QObject *parent = nullptr) : QStringListModel(parent)
                          {}
                          QHash<int, QByteArray> roleNames() const override
                          {
                              QHash<int, QByteArray> roles;
                      
                                  roles[Qt::EditRole] = QByteArrayLiteral("TestRole");
                                  roles[Qt::DisplayRole] = QByteArrayLiteral("AnotherRole");
                      
                              return roles;
                          }
                      };
                      
                      int main(int argc, char *argv[])
                      {
                      #if defined(Q_OS_WIN)
                          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                      #endif
                          QGuiApplication app(argc, argv);
                          QQmlApplicationEngine engine;
                          MyModel model;
                          QObject::connect(&model,&MyModel::dataChanged,[](const QModelIndex& idx){
                              qDebug() << "Changed: " << idx.row() << " " << idx.data().toString();
                          });
                          model.setStringList(QStringList{"aaa","bbb","ccc"});
                          engine.rootContext()->setContextProperty("mymodel", &model);
                          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                          if (engine.rootObjects().isEmpty())
                              return -1;
                          return app.exec();
                      }
                      
                      import QtQml 2.2
                      import QtQuick 2.7
                      import QtQuick.Window 2.2
                      import QtQuick.Controls 2.3
                      
                      Window {
                          visible: true
                          width: 640
                          height: 480
                          title: qsTr("Test Edit")
                      
                              ListView {
                                  anchors.fill: parent;
                                  width: 200; height: 250
                                  model: mymodel
                                  delegate: TextInput  {
                                      text: TestRole
                                      onEditingFinished: {
                                          TestRole = text
                                      }
                                  }
                              }
                      }
                      

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      B 1 Reply Last reply
                      1
                      • VRoninV VRonin

                        Are you sure you are inside delegate:?
                        This is a minimal working example:

                        #include <QGuiApplication>
                        #include <QQmlContext>
                        #include <QQmlApplicationEngine>
                        #include <QStringListModel>
                        #include <QDebug>
                        class MyModel : public QStringListModel{
                        public:
                            MyModel(QObject *parent = nullptr) : QStringListModel(parent)
                            {}
                            QHash<int, QByteArray> roleNames() const override
                            {
                                QHash<int, QByteArray> roles;
                        
                                    roles[Qt::EditRole] = QByteArrayLiteral("TestRole");
                                    roles[Qt::DisplayRole] = QByteArrayLiteral("AnotherRole");
                        
                                return roles;
                            }
                        };
                        
                        int main(int argc, char *argv[])
                        {
                        #if defined(Q_OS_WIN)
                            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                        #endif
                            QGuiApplication app(argc, argv);
                            QQmlApplicationEngine engine;
                            MyModel model;
                            QObject::connect(&model,&MyModel::dataChanged,[](const QModelIndex& idx){
                                qDebug() << "Changed: " << idx.row() << " " << idx.data().toString();
                            });
                            model.setStringList(QStringList{"aaa","bbb","ccc"});
                            engine.rootContext()->setContextProperty("mymodel", &model);
                            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                            if (engine.rootObjects().isEmpty())
                                return -1;
                            return app.exec();
                        }
                        
                        import QtQml 2.2
                        import QtQuick 2.7
                        import QtQuick.Window 2.2
                        import QtQuick.Controls 2.3
                        
                        Window {
                            visible: true
                            width: 640
                            height: 480
                            title: qsTr("Test Edit")
                        
                                ListView {
                                    anchors.fill: parent;
                                    width: 200; height: 250
                                    model: mymodel
                                    delegate: TextInput  {
                                        text: TestRole
                                        onEditingFinished: {
                                            TestRole = text
                                        }
                                    }
                                }
                        }
                        
                        B Offline
                        B Offline
                        Bremenpl
                        wrote on last edited by Bremenpl
                        #14

                        @VRonin Hmmm... Here is the thing- I am inside itemDelegate not delegate. delegate is available in different QtQuick.Controls version. If I switch to it, I will no longer have my other components available, ie Popup. Isnt itemDelegate and delegate the same thing?

                        Look at the docs, TableView has no delegate member: https://doc.qt.io/qt-5/qml-qtquick-controls-tableview.html#rowDelegate-prop

                        lprzenioslo.zut.edu.pl

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by VRonin
                          #15

                          Looks like you are still using Qt Quick controls 1.
                          Qt Quick controls 2 is already 3 years old.

                          If I switch to it, I will no longer have my other components available, ie Popup

                          Qt Quick Controls 2 has https://doc.qt.io/qt-5/qml-qtquick-controls2-popup.html

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          B 1 Reply Last reply
                          1
                          • VRoninV VRonin

                            Looks like you are still using Qt Quick controls 1.
                            Qt Quick controls 2 is already 3 years old.

                            If I switch to it, I will no longer have my other components available, ie Popup

                            Qt Quick Controls 2 has https://doc.qt.io/qt-5/qml-qtquick-controls2-popup.html

                            B Offline
                            B Offline
                            Bremenpl
                            wrote on last edited by
                            #16

                            @VRonin QtQuick.Controls 2.5 do have delegate but dont have TableViewColumn which I am using for columns setting. Is there a better way now?

                            lprzenioslo.zut.edu.pl

                            VRoninV 1 Reply Last reply
                            0
                            • B Bremenpl

                              @VRonin QtQuick.Controls 2.5 do have delegate but dont have TableViewColumn which I am using for columns setting. Is there a better way now?

                              VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by
                              #17

                              @Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:

                              Is there a better way now?

                              @VRonin said in QAbstractTableModel + QML TableView: How to call setData?:

                              Looks like columns and roles are the same thing here but since 1 year ago they are now 2 distinct things (as they are in QtWidgets): https://blog.qt.io/blog/2018/08/29/tableview/

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              B 1 Reply Last reply
                              1
                              • VRoninV VRonin

                                @Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:

                                Is there a better way now?

                                @VRonin said in QAbstractTableModel + QML TableView: How to call setData?:

                                Looks like columns and roles are the same thing here but since 1 year ago they are now 2 distinct things (as they are in QtWidgets): https://blog.qt.io/blog/2018/08/29/tableview/

                                B Offline
                                B Offline
                                Bremenpl
                                wrote on last edited by
                                #18

                                @VRonin Ok... So now everything I got ahead of with styleData is no longer working. This time I am really confused. Those links threat about animations the most and what I need for now is to be able to send/ receive data from the C++ model. Is there any example for that?

                                lprzenioslo.zut.edu.pl

                                VRoninV 1 Reply Last reply
                                0
                                • B Bremenpl

                                  @VRonin Ok... So now everything I got ahead of with styleData is no longer working. This time I am really confused. Those links threat about animations the most and what I need for now is to be able to send/ receive data from the C++ model. Is there any example for that?

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by
                                  #19

                                  @Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:

                                  So now everything I got ahead of with styleData is no longer working.

                                  What is styleData?

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  B 1 Reply Last reply
                                  1
                                  • VRoninV VRonin

                                    @Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:

                                    So now everything I got ahead of with styleData is no longer working.

                                    What is styleData?

                                    B Offline
                                    B Offline
                                    Bremenpl
                                    wrote on last edited by Bremenpl
                                    #20

                                    @VRonin It was available in itemDelegate: https://doc.qt.io/qt-5/qml-qtquick-controls-tableview.html#itemDelegate-prop
                                    One could use it to locate itself in the table as well as read model data.

                                    lprzenioslo.zut.edu.pl

                                    VRoninV 1 Reply Last reply
                                    0
                                    • B Bremenpl

                                      @VRonin It was available in itemDelegate: https://doc.qt.io/qt-5/qml-qtquick-controls-tableview.html#itemDelegate-prop
                                      One could use it to locate itself in the table as well as read model data.

                                      VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by
                                      #21

                                      @Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:

                                      One could use it to locate itself in the table as well as read model data.

                                      now you don't need it anymore. you just access role names directly to read data and you can use index, row and column directly to locate yourself within the model

                                      Btw, this doesnt call setData:

                                      Yes it does. If the dataChanged signal is not enough to convince you you can add the below to MyModel

                                      bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override{
                                              qDebug() << "Called setData";
                                              return QStringListModel::setData(index,value,role);
                                          }
                                      

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      B 1 Reply Last reply
                                      1
                                      • VRoninV VRonin

                                        @Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:

                                        One could use it to locate itself in the table as well as read model data.

                                        now you don't need it anymore. you just access role names directly to read data and you can use index, row and column directly to locate yourself within the model

                                        Btw, this doesnt call setData:

                                        Yes it does. If the dataChanged signal is not enough to convince you you can add the below to MyModel

                                        bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override{
                                                qDebug() << "Called setData";
                                                return QStringListModel::setData(index,value,role);
                                            }
                                        
                                        B Offline
                                        B Offline
                                        Bremenpl
                                        wrote on last edited by
                                        #22

                                        @VRonin Ok, you are right! Thanks.
                                        It feels like we are really close this time. So this is the components now:

                                        		TableView
                                        		{
                                        			anchors.fill: parent;
                                        			model: tableModel;
                                        
                                        			delegate: TextInput
                                        			{
                                        				text: Name;
                                        
                                        				onEditingFinished:
                                        				{
                                        					Name = text
                                        				}
                                        			}
                                        		}
                                        

                                        My columns and roles are still mixed together because of the obsolete behavior- this needs to be disconnected. In this case, what should the roleNames override method return actually?

                                        lprzenioslo.zut.edu.pl

                                        1 Reply Last reply
                                        0
                                        • VRoninV Offline
                                          VRoninV Offline
                                          VRonin
                                          wrote on last edited by VRonin
                                          #23

                                          @Bremenpl said in QAbstractTableModel + QML TableView: How to call setData?:

                                          hat should the roleNames override method return actually?

                                          I would not override it at all and just use the default edit

                                          				text: edit
                                          
                                          				onEditingFinished:
                                          				{
                                          					edit = text
                                          				}
                                          

                                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                          ~Napoleon Bonaparte

                                          On a crusade to banish setIndexWidget() from the holy land of Qt

                                          B 1 Reply Last reply
                                          1

                                          • Login

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