Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Deleting rows from QTableWidget
Qt 6.12 LTS is out! See what's new in the release blog

Deleting rows from QTableWidget

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 157 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
    Jabik
    wrote last edited by
    #1

    Hello guys, can someone help me?
    I am trying to delete row from a QTableWidget.
    So, i have:

    food_table = new QTableWidget(1, 2);
    

    then, it connects to a slot

    connect(food_table, SIGNAL(cellChanged(int,int)), this, SLOT(checkTheCells(int,int)));
    

    slot code:

    void InsertNewEatingDialog::checkTheCells(int changed_row, int changed_column)
    {
        int total_rows{food_table->rowCount()};
        if(rowIsEmpty(changed_row) && total_rows > 1) food_table->removeRow(changed_row);
        if(tableIsFull()) food_table->insertRow(total_rows);
    }
    

    rowIsEmpty() and tableIsFull() are just bool member functions.
    And it all works, but when the code running, when the row was deleted, i receive a message in qt developer's terminal, which says:

    QAbstractItemView::commitData called with an editor that does not belong to this view
    

    and then the program continues to run normally until the next row deleting. Then that message again.
    I noticed that if I replace 1 with 2 in the line

    if(rowIsEmpty(changed_row) && total_rows > 1) food_table->removeRow(changed_row);
    

    it works without any messages.
    Can someone explain me the cause of the error and a possible solution?

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

      Hi and welcome to devnet,

      Any chances you have an editor opened in the row you are trying to delete ?
      How are you editing your cells ?
      How are you deleting your cells ?

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

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Any chances you have an editor opened in the row you are trying to delete ?
        How are you editing your cells ?
        How are you deleting your cells ?

        J Offline
        J Offline
        Jabik
        wrote last edited by
        #3

        @SGaist said in Deleting rows from QTableWidget:

        welcome to devnet,

        thx

        @SGaist said in Deleting rows from QTableWidget:

        How are you editing your cells ?

        standard editing with left click (or dbl click) and entering the value, then "enter", or just click somewhere outside the editing cell

        @SGaist said in Deleting rows from QTableWidget:

        How are you deleting your cells ?
        i'm dbl clicking the cell, then just "delete". then "enter" or click somewhere outside the cell. I understand, that this action just makes QTableWidgetItem's text empty and nothing more.

        but any of these actions emits signal cellChanged(int, int).
        this signal is connected with my own slot checkTheCells(int, int);

        then, inside the signal i'm checking if row is empty. Empty means, that either:

        • every cell item is empty
        QTableWidgetItem* item; 
        if(!item){...}
        

        -or

        item->text().size() == 0
        

        if all cells in a row are empty, then i'm removing the whole row, because i don't need it

        if(rowIsEmpty(changed_row) && total_rows > 1) food_table->removeRow(changed_row);
        

        that's all

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote last edited by
          #4

          The problem is that the editor is not yet closed when cellChanged()/dataChanged() is emitted and you're killing the row where the editor lives in. Use a Qt::QueuedConnection:

          int main(int argc, char* argv[])
          {
              QApplication a(argc, argv);
              QTableWidget tw(10, 10);
              QObject::connect(&tw, &QTableWidget::cellChanged, &tw, [&](int row, int) {
                  tw.removeRow(row);
                  }, Qt::QueuedConnection);
              tw.show();
              return a.exec();
          }
          

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          J 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            The problem is that the editor is not yet closed when cellChanged()/dataChanged() is emitted and you're killing the row where the editor lives in. Use a Qt::QueuedConnection:

            int main(int argc, char* argv[])
            {
                QApplication a(argc, argv);
                QTableWidget tw(10, 10);
                QObject::connect(&tw, &QTableWidget::cellChanged, &tw, [&](int row, int) {
                    tw.removeRow(row);
                    }, Qt::QueuedConnection);
                tw.show();
                return a.exec();
            }
            
            J Offline
            J Offline
            Jabik
            wrote last edited by
            #5

            @Christian-Ehrlicher
            Yeah, that works perfectly.
            But to understand, what is "the editor"? Is it an object inside QT library, or is it an external process in OS?
            How can I interact with it?
            Or just, where to read about it?

            Christian EhrlicherC 1 Reply Last reply
            0
            • J Jabik

              @Christian-Ehrlicher
              Yeah, that works perfectly.
              But to understand, what is "the editor"? Is it an object inside QT library, or is it an external process in OS?
              How can I interact with it?
              Or just, where to read about it?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote last edited by
              #6

              @Jabik said in Deleting rows from QTableWidget:

              But to understand, what is "the editor"? Is it an object inside QT library, or is it an external process in OS?

              When you edit a cell, an editor is created so you can enter the new value.
              See https://doc.qt.io/qt-6/model-view-programming.html#delegate-classes

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              J 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Jabik said in Deleting rows from QTableWidget:

                But to understand, what is "the editor"? Is it an object inside QT library, or is it an external process in OS?

                When you edit a cell, an editor is created so you can enter the new value.
                See https://doc.qt.io/qt-6/model-view-programming.html#delegate-classes

                J Offline
                J Offline
                Jabik
                wrote last edited by
                #7

                @Christian-Ehrlicher
                Ok, thank you, i'll read it

                1 Reply Last reply
                0
                • J Jabik has marked this topic as solved

                • Login

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