[BUG]QTableWidget crashes on removal of rows



  • 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



  • 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?



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



  • 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 :)



  • 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



  • 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 :)



  • Now only single row gets deleted instead of three rows!



  • 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]



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



  • 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.