April 8, 2011

Ruzik Ruzik
Lab Rat
293 posts

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?

  1.  QList<QTableWidgetItem *> itemList = tableWidget->selectedItems();
  2.  int i = itemList.count()/tableWidget->columnCount();
  3.  int start = tableWidget->row(itemList[0]);
  4.  for (int a=start;a<=start+i;a++)
  5.  {
  6.   tableWidget->removeRow(a);
  7.  }

Sorry for the bad english and advance thanks for the help!

6 replies

April 8, 2011

Andre Andre
Area 51 Engineer
6031 posts

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:

  1. QSet<int> selectedRows; //we use a set to prevent doubles
  2. QList<QTableWidgetItem*> itemList = tableWidget->selectedItems();
  3. foreach(QTableWidgetItem item, itemList)
  4.     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.

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 8, 2011

Ruzik Ruzik
Lab Rat
293 posts

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)

April 8, 2011

Andre Andre
Area 51 Engineer
6031 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 8, 2011

Ruzik Ruzik
Lab Rat
293 posts

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

  1.  QSet<int> selectedRows; //we use a set to prevent doubles
  2.  QList<QTableWidgetItem*> itemList = tableWidget->selectedItems();
  3.  foreach(item, itemList)
  4.  selectedRows.insert(item->row());
  5.  //get a list, and sort it big to small
  6.  QList<int> rows = selectedRows.toList();
  7.  qSort(rows.begin(), rows.end());
  8.  //now actually do the removing:
  9.  foreach(int row, rows)
  10.   tableWidget->removeRow(row);

April 8, 2011

Andre Andre
Area 51 Engineer
6031 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 8, 2011

Volker Volker
Robot Herder
5428 posts

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

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

 
  ‹‹ [Split] Font color in QTextEdit      Memory Allocation Error in Qt Creator ››

You must log in to post a reply. Not a member yet? Register here!