Delete rows in the tableView



  • Hello? i have the code of deleting rows in the tableView, but it is work wrong
    What i do wrong?
    @ QList<QTableWidgetItem *> itemList = tableWidget->selectedItems();
    int i = itemList.count()/tableWidget->columnCount();
    int start = tableWidget->row(itemList[0]);
    for (int a=start;a<=start+i;a++)
    {
    tableWidget->removeRow(a);
    }@
    Sorry for the bad english and advance thanks for the help!



  • Your loop looks like a bit of a mess.
    I am guessing you want to remove all rows in which an item is selected, right?

    There are multiple ways to do this, and this is just one:

    Your first challenge is to find those rows. What you could do is this:

    @
    QSet<int> selectedRows; //we use a set to prevent doubles
    QList<QTableWidgetItem*> itemList = tableWidget->selectedItems();
    foreach(QTableWidgetItem item, itemList)
    selectedRows.insert(item.row());
    @

    Next step, is to actually remove them. Observe that you need to start removing from the bottom to the top, otherwise the row numbers of the rows that you still need to remove later will change, and that gets messy.

    @
    //get a list, and sort it big to small
    QList<int> rows = selectedRows.toList();
    qSort(rows.begin(), rows.end(), qGreater<int>);
    //now actually do the removing:
    foreach(int row, rows)
    tableWidget->removeRow(row);
    @



  • I have 2 error when i use your code
    Error 2 error C2275: qGreater <T>: invalid use of this type as an expression c: \ Documents and Settings \ rustam \ My Documents \ Visual Studio 2008 \ Projects \ RizekActionManager \ RizekActionManager \ rizekactionmanager.cpp 253
    Error 1 error C2440: initialization: can not convert 'QTableWidgetItem * const' to 'QTableWidgetItem' c: \ Documents and Settings \ rustam \ My Documents \ Visual Studio 2008 \ Projects \ RizekActionManager \ RizekActionManager \ rizekactionmanager.cpp 249
    Maybe error occurs because the compiler(i use MSVS 2008)



  • No, it is very possible there are errors in the code above. I typed it directly in the forum, and I did not test it. The code was merely to illustrate a point, to make clear how to do what you want. I do not claim it will run unmodified.



  • If use your code, it does not remove all the even elements
    I changed the code a bit to avoid mistakes, but I do not think that it is strongly influenced
    @ QSet<int> selectedRows; //we use a set to prevent doubles
    QList<QTableWidgetItem*> itemList = tableWidget->selectedItems();
    QTableWidgetItem * item;
    foreach(item, itemList)
    selectedRows.insert(item->row());
    //get a list, and sort it big to small
    QList<int> rows = selectedRows.toList();
    qSort(rows.begin(), rows.end());
    //now actually do the removing:
    foreach(int row, rows)
    tableWidget->removeRow(row);@



  • I think you will find that you are now removing the wrong rows...



  • use (spot the parentheses after the <int>).

    @
    qSort(rows.begin(), rows.end(), qGreater<int>());
    @


Log in to reply
 

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