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. QAbstractListModel updation issue after rows remove.
Forum Updated to NodeBB v4.3 + New Features

QAbstractListModel updation issue after rows remove.

Scheduled Pinned Locked Moved Solved QML and Qt Quick
20 Posts 4 Posters 3.4k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #11

    The way you re-implemented the run method breaks the worker objects paradigm. The idea is to have an event loop running which is not the case anymore, so call the base class implementation after your qDebug statement or delete the method completely.

    Next question, why go that convoluted way to delete one row ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    R 1 Reply Last reply
    0
    • dheerendraD dheerendra

      I'm surprised. Following line in main.cpp compiles ?

      TableModel pTableModel = new TableModel();

      Now,
      When you select the element & click on "Remove Row", what is the output you see in "Application Window" ? Do you see any warning ?

      R Offline
      R Offline
      Remya
      wrote on last edited by
      #12

      @dheerendra
      Thanks for your replay.
      Yes i get two warnings please see below,

      1. QObject::connect: No such signal ThreadHandler::quitThread() in ../TestAppModel/main.cpp:148 // This part forgot to declare signal quitThread() in sample application.

      2. QObject::connect: Cannot queue arguments of type 'QQmlChangeSet'
        (Make sure 'QQmlChangeSet' is registered using qRegisterMetaType().)

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #13

        This is where everything going for haywire. You are trying to push the changes from different thread. Due to this you are View is not updated properly. You have two options.

        1. Don't update the model directly in another thread. If you want to do it use signals/slots to update the model from different thread.
        2. As @SGaist already asked you, why you are doing round-way of updating/deleting just a row of elements of model. In your case just use the single threaded approach.

        Comment everything in RemoveRows(..) method. Place the following code & see it works or not. This is single threaded approach.

        ThreadHandler* pHandler = new ThreadHandler();
        pHandler->setIndex(index);
        pHandler->setTableMOdel(m_pTableModel);
        pHandler->execute();
        

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        R 1 Reply Last reply
        0
        • SGaistS SGaist

          The way you re-implemented the run method breaks the worker objects paradigm. The idea is to have an event loop running which is not the case anymore, so call the base class implementation after your qDebug statement or delete the method completely.

          Next question, why go that convoluted way to delete one row ?

          R Offline
          R Offline
          Remya
          wrote on last edited by
          #14

          @SGaist said in QAbstractListModel updation issue after rows remove.:

          convoluted

          Thanks for the comments.
          Yes i removed the run() method complete.
          Actually i need to delete row from the tableview using a thread.
          Is there any better method for that ?

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #15

            @Remya said in QAbstractListModel updation issue after rows remove.:

            Actually i need to delete row from the tableview using a thread.

            Again: why ?

            A better way ? Don't use threads unless you have a very good reason (and "I need to" doesn't count as a good reason until explained properly)

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply
            2
            • dheerendraD dheerendra

              This is where everything going for haywire. You are trying to push the changes from different thread. Due to this you are View is not updated properly. You have two options.

              1. Don't update the model directly in another thread. If you want to do it use signals/slots to update the model from different thread.
              2. As @SGaist already asked you, why you are doing round-way of updating/deleting just a row of elements of model. In your case just use the single threaded approach.

              Comment everything in RemoveRows(..) method. Place the following code & see it works or not. This is single threaded approach.

              ThreadHandler* pHandler = new ThreadHandler();
              pHandler->setIndex(index);
              pHandler->setTableMOdel(m_pTableModel);
              pHandler->execute();
              
              R Offline
              R Offline
              Remya
              wrote on last edited by
              #16

              @dheerendra
              Thanks a lot.
              I did signal/slot updation from different thread.
              Works fine for me.

              1 Reply Last reply
              0
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #17

                Cool. Still we wonder why update from threads unless you strong requirement. It could be some experiment as well. Also we need to look at the warning, errors which come when we run the application. It was clearly telling the issue.

                Now it is working & made it move the issue to solved state. You can share your example also so that others benefit from the same.

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                0
                • SGaistS SGaist

                  @Remya said in QAbstractListModel updation issue after rows remove.:

                  Actually i need to delete row from the tableview using a thread.

                  Again: why ?

                  A better way ? Don't use threads unless you have a very good reason (and "I need to" doesn't count as a good reason until explained properly)

                  R Offline
                  R Offline
                  Remya
                  wrote on last edited by
                  #18

                  @SGaist
                  Thanks for your replay.
                  Actually my real application need to do some time consuming task when we remove rows from the table view .
                  Any way i did that using signal/slot, that work fine now.
                  Thank you.

                  1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by
                    #19

                    Please move the issue to solved state as well.

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    http://www.pthinks.com

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Remya
                      wrote on last edited by
                      #20

                      Please see the sample below. This works !!

                      //###############################Fiile : main.cpp

                      #include <QGuiApplication>
                      #include <QQmlApplicationEngine>
                      #include <QAbstractTableModel>
                      #include <QtQml/QQmlContext>
                      #include <qdebug.h>
                      #include <QThread>

                      class TableModel : public QAbstractTableModel
                      {
                      Q_OBJECT
                      QList <QString> m_List;

                      public:

                      TableModel()
                      {
                          for(int index =0; index < 100 ; index++)
                          {
                              insertValue(QString::number(index));
                          }
                      }
                      
                      Q_INVOKABLE int rowCount(const QModelIndex & = QModelIndex()) const override
                      {
                          return  m_List.count();
                      }
                      
                      
                      Q_INVOKABLE int columnCount(const QModelIndex & = QModelIndex()) const override
                      {
                          return m_List.count();
                      }
                      
                      Q_INVOKABLE  QVariant data(const QModelIndex &index, int role) const override
                      {
                          switch (role) {
                      
                              case Qt::DisplayRole:
                                  {
                                  return QString("%1").arg(m_List[index.row()]);
                          }
                              default:
                                  break;
                          }
                          return QVariant();
                      }
                      
                      Q_INVOKABLE void insertValue(QString value)
                      {
                          beginInsertRows(QModelIndex(), rowCount(), rowCount());
                              m_List.append(value);
                          endInsertRows();
                      }
                      
                      
                      Q_INVOKABLE void deleteData(int rowIndex)
                      {
                          if (rowIndex <= this->m_List.count())
                          {
                              this->beginRemoveRows(QModelIndex(), rowIndex, rowIndex);
                              this->m_List.removeAt(rowIndex);
                              this->endRemoveRows();
                              emitDataChanged();
                          }
                      }
                      
                      void emitDeleteRow(int rowIndex)
                      {
                          emit removeRow(rowIndex);
                      }
                      

                      QHash<int, QByteArray> roleNames() const override
                      {
                      return { {Qt::DisplayRole, "display"} };
                      }

                      QVector<int> getRoles() const
                      {
                      QVector<int> roles;
                      roles.append(Qt::DisplayRole);
                      return roles;
                      }

                      void emitDataChanged()
                      {
                      QModelIndex start = index(0,0);
                      QModelIndex end = index(rowCount() - 1, 0);
                      emit dataChanged(start, end, getRoles());
                      }

                      signals:
                      void removeRow(int rowIndex);

                      };

                      class ThreadHandler : public QObject
                      {
                      Q_OBJECT
                      TableModel* m_pTableModel;
                      int m_Index;
                      signals:
                      void quitThread();

                      public:
                      void setTableMOdel(TableModel* pTableModel)
                      {
                      m_pTableModel = pTableModel;
                      }

                      void setIndex(int index)
                      {
                          m_Index = index;
                      }
                      

                      public slots:
                      void execute()
                      {
                      m_pTableModel->emitDeleteRow(m_Index);
                      emit quitThread();
                      }

                      };

                      class ButtonOperation : public QObject
                      {
                      Q_OBJECT
                      TableModel* m_pTableModel;

                      public:

                      void setTableModel(TableModel* tableModel)
                      {
                          m_pTableModel = tableModel;
                      }
                      
                      Q_INVOKABLE void RemoveRows(int index)
                      {
                          QThread* pPThread = new QThread();
                          ThreadHandler* pHandler = new ThreadHandler();
                          pHandler->setIndex(index);
                          pHandler->setTableMOdel(m_pTableModel);
                      
                          pHandler->moveToThread(pPThread);
                      
                          connect(pPThread, SIGNAL(started()), pHandler, SLOT(execute()), Qt::DirectConnection);
                          connect(pHandler, SIGNAL(quitThread()), pPThread, SLOT(quit()), Qt::DirectConnection);
                      
                          // Automatically delete pPThread and pHandler after the work is done.
                          connect(pPThread, SIGNAL(finished()), pPThread, SLOT(deleteLater()), Qt::DirectConnection);
                          connect(pPThread, SIGNAL(finished()), pHandler, SLOT(deleteLater()), Qt::DirectConnection);
                      
                          pPThread->start();
                      }
                      

                      };

                      int main(int argc, char argv[])
                      {
                      QGuiApplication app(argc, argv);
                      TableModel
                      pTableModel = new TableModel();
                      QQmlApplicationEngine engine;
                      QQmlContext* context = engine.rootContext();
                      context->setContextProperty("TableModel", pTableModel);
                      ButtonOperation* pButtonOperation = new ButtonOperation();
                      pButtonOperation->setTableModel(pTableModel);
                      context->setContextProperty("ButtonOperation", pButtonOperation);
                      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                      return app.exec();
                      }

                      #include "main.moc"

                      ///##############################File main.qml

                      import QtQuick 2.4
                      import QtQuick.Controls 1.2

                      ApplicationWindow {
                      property int currentIndex: 0
                      id: window
                      visible: true
                      title: "Table View Example"
                      width: 600
                      height: 800

                      TableView {
                          id: tableView
                          width: 400
                          height: 500
                          anchors.centerIn: parent
                          clip: true
                      
                          model: TableModel
                      
                          TableViewColumn {
                              role: "display"
                              title: "Title"
                              width: 180
                              resizable: false
                              movable: false
                              delegate: Item {
                                  anchors.fill: parent
                                  Text {
                                      text: styleData.value
                                      verticalAlignment: Text.AlignVCenter
                                      horizontalAlignment: Text.AlignLeft
                                      elide: Text.ElideRight
                                      width: 160
                                      height: parent.height
                                      x:8
                                  }
                              }
                          }
                      
                          selection.onSelectionChanged:
                                  {
                                      currentIndex = tableView.currentRow;
                                      console.log("selected Row " + currentIndex)
                                   }
                      }
                      
                      Connections{
                          target: TableModel
                          onRemoveRow:
                          {
                              TableModel.deleteData(rowIndex);
                          }
                      }
                      
                      
                      Button{
                          id: button
                          width: 100
                          height: 30
                          text: "Remove Row"
                          onClicked: {
                           console.log("Button Clicked")
                              ButtonOperation.RemoveRows(currentIndex);
                          }
                      }
                      

                      }

                      ///######################## TestAppModel.pro

                      QT += quick
                      CONFIG += c++11

                      DEFINES += QT_DEPRECATED_WARNINGS

                      SOURCES += main.cpp

                      RESOURCES += qml.qrc

                      QML_IMPORT_PATH =

                      QML_DESIGNER_IMPORT_PATH =

                      qnx: target.path = /tmp/$${TARGET}/bin
                      else: unix:!android: target.path = /opt/$${TARGET}/bin
                      !isEmpty(target.path): INSTALLS += target

                      HEADERS +=

                      Thank you all.

                      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