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. [BUG]QTableWidget crashes on removal of rows
Forum Updated to NodeBB v4.3 + New Features

[BUG]QTableWidget crashes on removal of rows

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 4.6k 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.
  • A Offline
    A Offline
    adnan
    wrote on last edited by
    #1

    I want to remove multiple rows from QTableWidget, this is my code:
    @void on_del_clicked()
    {
    QList<QTableWidgetItem *> items = ui->tableWidget->selectedItems();
    for(int i = 0;i<items.length(); i=i+3)
    {
    int row = items[i]->row();
    if(row>=0)
    {
    ui->tableWidget->removeRow(row);
    ui->tableWidget->setCurrentIndex(ui->tableWidget->model()->index(row,0));
    }
    }
    }@

    If i choose multiple rows using Ctrl, it works fine, but if I choose multiple entries by dragging mouse, it crashes. It only crashes when selected items are multiple of 3

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Oke,
      Think if you look hard again and read your own comments above...............only crashed when multiple of 3.....hmm, there is (for me) an unexplained i= i + 3 in the for loop. Why is this? Might have something to do with it?

      Greetz, Jeroen

      1 Reply Last reply
      0
      • A Offline
        A Offline
        adnan
        wrote on last edited by
        #3

        There are 3 columns in each row and every column requires one item, so to move to next row i=i+3

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mmallad
          wrote on last edited by
          #4

          the problem is on i = i +3 though you have done this to move to next row. But when you select items by dragging if its not selected in sequence like each item in every rows should be selected otherwise i = i+3 wont work. I hope you get it :)

          Dpak Malla

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adnan
            wrote on last edited by
            #5

            My default selection behaviour is "select rows", so just by clicking on any row all the items are select. The code works fine this way using Ctrl for multiple selection, even for multiples of 3, the problem is with selection by dragging.

            @qDebug()<<items.length();@
            displays 9, which means all items are being selected

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mmallad
              wrote on last edited by
              #6

              Please change your code to
              @
              ui->tableWidget->setCurrentIndex(ui->tableWidget->model()->index(row,0));
              ui->tableWidget->removeRow(row);
              @

              First set current index and remove row :) Because we are trying to set current index of deleted row. It wont crash :)

              Dpak Malla

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adnan
                wrote on last edited by
                #7

                Now only single row gets deleted instead of three rows!

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mmallad
                  wrote on last edited by
                  #8

                  But I had tested it. It worked for me very well after changing the two lines. Also no need to check for condition row >= 0 beacuse row is always 0 equal or greater.
                  [quote author="adnan" date="1362258443"]Now only single row gets deleted instead of three rows![/quote]

                  Dpak Malla

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    adnan
                    wrote on last edited by
                    #9

                    Its not working, if i select three rows by dragging. It works only if I use Ctrl for selecting multiple rows.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      adnan
                      wrote on last edited by
                      #10

                      It seems to be a bug in QTableWidget. The following program confirms it:

                      @for(int i = 0;i<items.length(); i=i+3)
                      {
                      row = items[i]->row();
                      qDebug()<<"item number: "<< i<<" row: "<<row;
                      }@

                      If selection is made using Ctrl, suppose first three rows are selected:
                      Output is :
                      @item number: 0 row: 0
                      item number: 3 row: 1
                      item number: 6 row: 2 @

                      If selection is made by dragging mouse:
                      Output is:
                      @
                      item number: 0 row: 0
                      item number: 3 row: 0
                      item number: 6 row: 0 @
                      It happens only when number of rows are multiple of number of columns

                      Now, I found an alternative way, which works:
                      @QList<QTableWidgetItem *> items = ui->tableWidget->selectedItems();
                      while(!items.isEmpty())
                      {
                      ui->tableWidget->removeRow(items[0]->row(););
                      items = ui->tableWidget->selectedItems();
                      }@

                      Anyways, thanks for your help!

                      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