How to delete all rows from QTableWidget - c++

I am trying to delete all rows from a QTableWidget . Here is what I tried.
for ( int i = 0; i < mTestTable->rowCount(); ++i )
{
mTestTable->removeRow(i);
}
I had two rows in my table. But this just deleted a single row. A reason could be that I did not create the the table with a fixed table size. The Qt Documentation for rowCount() says,
This property holds the number of rows in the table.
By default, for a table constructed without row and column counts,
this property contains a value of 0.
So if that is the case, what is the best way to remove all rows from table?

Just set the row count to 0 with:
mTestTable->setRowCount(0);
it will delete the QTableWidgetItems automatically, by calling removeRows as you can see in QTableWidget internal model code:
void QTableModel::setRowCount(int rows)
{
int rc = verticalHeaderItems.count();
if (rows < 0 || rc == rows)
return;
if (rc < rows)
insertRows(qMax(rc, 0), rows - rc);
else
removeRows(qMax(rows, 0), rc - rows);
}

I don't know QTableWidget but your code seems to have a logic flaw. You are forgetting that as you go round the loop you are decreasing the value of mTestTable->rowCount(). After you have removed one row, i will be one and mTestTable->rowCount() will also be one, so your loop stops.
I would do it like this
while (mTestTable->rowCount() > 0)
{
mTestTable->removeRow(0);
}

AFAIK setRowCount(0) removes nothing. Objects are still there, but no more visible.
yourtable->model()->removeRows(0, yourtable->rowCount());

QTableWidget test;
test.clear();
test.setRowCount( 0);

The simple way to delete rows is to set the row count to zero. This uses removeRows() internally.
table->setRowCount(0);
You could also clear the content and then remove all rows.
table->clearContents();
table->model()->removeRows(0, table->rowCount());
Both snippets leave the headers untouched!
If you need to get rid of headers, too, you could switch from clearContents() to clear().

In order to prevent an app crash, disconnect all signals from the QTableView.
// Deselects all selected items
ui->tableWidget->clearSelection();
// Disconnect all signals from table widget ! important !
ui->tableWidget->disconnect();
// Remove all items
ui->tableWidget->clearContents();
// Set row count to 0 (remove rows)
ui->tableWidget->setRowCount(0);

Look this post : http://forum.qt.io/topic/1715/qtablewidget-how-to-delete-a-row
QList<QTableWidgetItem*> items = table.findItems(.....);
QMap<int, int> rowsMap;
for(int i = 0; i < items.count(); i++{
rowsMap[items.at(i).row()] = -1; //garbage value
}
QList<int> rowsList = rowsMap.uniqueKeys();
qSort(rowsList);
//Now go through your table and delete rows in descending order as content would shift up and hence cannot do it in ascending order with ease.
for(int i = rowList.count() - 1; i >= 0; i--){
table.removeRow(rowList.at(i));
}

You can just add empty item model (QStandardItemModel) to your QTableView (myTableView):
itemModel = new QStandardItemModel;
ui->myTableView->setModel(itemModel);

In python you can just set the rowCount to zero and that'll work!
tableWidget.setRowCount(0)
This code is tested with PySide6. Hope it will work for PyQt5 and PyQt6 too.

Your code does not delete last row.
Try this one.
int totalRow = mTestTable->rowCount();
for ( int i = 0; i < totalRow ; ++i )
{
mTestTable->removeRow(i);
}
In your code, on the first time, rowCount() have value 2 and value of the i is 0, so its delete 1st row,
But on the second time value of i incremented with 1, but rowCount() return the updated row count which is now 1, so, it does not delete the last row.
Hope now you ll be clear.

Removes all items not in the headers from the view. This will also remove all selections. The table dimensions stay the same.
void QTableWidget::clearContents()
Removes all items in the view. This will also remove all selections and headers.
void QTableWidget::clear()

This works for me:
for i in reversed(range(self.tableWidget.rowCount())):
self.tableWidget.removeRow(i)

Related

How to set a new item in tabview Qt, and save the previous ones

When I use Qt tableView, I can just set item in the first row. When I click add, it will cover the former, but not set a new item. Maybe my slot function is incorrect. But I don't know how to handle it.
void Widget::on_addButton_clicked()
{ int i = 0;
EditDialog editDialog(this);
if(editDialog.exec() == 1)
{
model->setItem(i,0,new QStandardItem(editDialog.getID()));
model->setItem(i,1,new QStandardItem(editDialog.getPriority()));
model->setItem(i,2,new QStandardItem(editDialog.getTime()));
}
i++;
}
Please, have a look into doc. QStandardItemModel::setItem():
Sets the item for the given row and column to item. The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item. The previous item at the given location (if there was one) is deleted.
(Emphasizing is mine.)
If a row shall be inserted before end of table (e.g. as first), then it's necessary to do this explicitly.
This can be achieved by calling QStandardItemModel::insertRow() before setting the items.
This could e.g. look like this:
// fills a table model with sample data
void populate(QStandardItemModel &tblModel, bool prepend)
{
int row = tblModel.rowCount();
if (prepend) tblModel.insertRow(0);
for (int col = 0; col < NCols; ++col) {
QStandardItem *pItem = new QStandardItem(QString("row %0, col %1").arg(row).arg(col));
tblModel.setItem(prepend ? 0 : row, col, pItem);
}
}
I took this from an older post of mine. The complete sample can be found in my answer to SO: Stop QTableView from scrolling as data is added above current position.

How to delete selected row of a List Control in MFC?

I want to delete selected row of list control in MFC.
I have created a Delete Button, So If any row (it could be one or more than one row) is/are selected and I press delete button that/those rows should be deleted.
If lets say there are 100 rows and I select rows from 50-60, all the rows in this range should be deleted and rest of rows should have indexes from 1 to 90. means indexing should be proper after deletion also.
Adapted from this MSDN article:
UINT i, uSelectedCount = m_myListCtrl.GetSelectedCount();
int nItem;
if (uSelectedCount > 0)
for (i=0; i < uSelectedCount; i++)
{ nItem = m_myListCtrl.GetNextItem(-1, LVNI_SELECTED);
ASSERT(nItem != -1);
m_myListCtrl.DeleteItem(nItem);
}
When deleting a multiple selection having several items I prefer to do it like this:
int nItem = -1;
while ((nItem = m_list.GetNextItem(nItem, LVNI_SELECTED)) != -1)
{
if (m_list.DeleteItem(nItem))
nItem--;
}
Notice the important nItem--; line
UPDATE
I had to give up from this approach as the ItemData of an element gots fucked up. If I remove the nth element then the n+1 element will be my new nth. That element has a completely screwed up Itemdata.
UPDATE 2
I also tried with
int nItem = -1;
while ((nItem = m_list.GetNextItem(-1, LVNI_SELECTED)) != -1)
{
m_list.DeleteItem(nItem);
}
This approach also has the problem of screwing the Itemdata I reported before.
The following approach worked perfecly for me:
std::stack< int > items;
int nItem = -1;
while ((nItem = myListCtrl.GetNextItem(nItem, LVNI_SELECTED)) != -1)
{
items.push(nItem);
}
bool removed = false;
while (!items.empty())
{
nItem = items.top();
if (myListCtrl.DeleItem(nItem))
removed = true;
items.pop();
}
if (removed)
// update some application state;
Explanation:
When you remove things from the end to the start, you do not have to worry about the validity of positions. As the CListCtrl does not provide a GetPrevItem or any other way to get items in the reverse order, you need to store them in a collection where you can have that reverse order.
The most practical way to do it is to use a stack. Due to the way it works, you will put things in there in the normal order, and when you retrieve things they are automatically in reverse order.

QCombobox doesn't select when changing currentIndex

In my constructor I connect to a Sqlite Database and read "Categories" (QStrings) from it.
I store them in a QList. I checked via debugger if they're empty, but everything is fine.
The int currCategoryIndex is set to 0 in my initializer list. And since QComboboxes start indexing from 0 it should be the first item.
An extract from my constructor:
dm.readDB_C(c_list); //reads into c_list
if(c_list.count() > 0) //has 1 or more items
updateCategories();
This is the part where I read the database, check if it's empty and if not call a function which adds those categories to a QComboBox.
updateCategories() function :
void MainWindow::updateCategories()
{
for(int i = 0; i < c_list.count(); i++){
if(ui->cmbCategory->findText(c_list[i]) != -1){ //-1 means "not found"
continue;
} else {
ui->cmbCategory->addItem(c_list[i]); //Add to QCombobox
}
}
ui->cmbCategory->setCurrentIndex(currCategoryIndex); //Should be the first item
}
I have all items in my QCombobox but none is selected. I have to click the box and select one myself. That's not supposed to happen.
What is wrong? Why doesn't it select one itself?
Edit:
currentIndexChanged signal:
void MainWindow::on_cmbCategory_currentIndexChanged(int index){
currCategoryIndex = index;
}
Perhaps the first item is empty?
Since you only add items to QComboBox, it is possible that index 0 is (from the beginning) an empty string.
try putting
ui->cmbCategory->removeItem(0);
in the beginning of updateCategories to check if it is the case
also, if currCategoryIndex is an index that does not exist (for example -1) QComboBox will also be empty (even if there is no empty string to choose) - in this case you can try to hardcode 0 in the function (if you want the item to always be the first one), or add additional check, for example:
if (0 > currentCategoryIndex || currentCategoryIndex > ui->cmbCategory->count())
currentCategoryIndex = 0

Find out which column is selected in a QTableWidget

I have a QTableWidget with SelectionMode set to SingleSelection, and SelectionBehavior set to SelectColumns. This means that only a single column can be selected.
But I later need to find out which column is selected, and the only functions I can use are selectedIndexes() or selectedItems(), both of which return entire lists, which is wasteful.
Is there a way to do this more efficiently?
Your approach with the selectedItems() was correct.
As QT cant know that you've set your widget to singlerow/column selection it offers those functions to return a QList<>.
in your case you can work on those by using .first().
Evne though I suggest to use the signals currentColumnChanged() to react in your application
( http://harmattan-dev.nokia.com/docs/library/html/qt4/qitemselectionmodel.html#currentColumnChanged )
you could always iterate over all columns of the selected row via selectionModel()->isColumnSelected()
( http://qt-project.org/doc/qt-4.8/qitemselectionmodel.html#isColumnSelected )
connect(tableWidget, SIGNAL(currentCellChanged(int,int,int,int), this, SLOT(onCellChanged(int,int,int,int)));
void Class::onCellChanged(int curRow, int curCol, int preRow, int preCol)
{
current_Col = curCol;
// curRow, preRow and preCol are unused
}
connect(tableWidget->selectionModel()
, SIGNAL(currentColumnChanged(QModelIndex,QModelIndex))
, SLOT(onColumnChanged(QModelIndex)));
...
void Class::onColumnChanged(const QModelIndex &index)
{
int col = index.column();
}
It seems that the function selectedRanges() does what I need. It returns a list of the selected ranges, but since it's a single column, this list would have only one item (so it's efficient, no big list need to be created).
int column = ui->tableWidget->selectedRanges().front().leftColumn();
currentColumn() returns an int of the current selected column.

Why object be deleted without obvious code?

In qt, I use tablewidget to store 100 rows. At first, I new tableWidgetItems to fill the rows.
As it runs, I set the items' propertities and no longer 'new'.
But I find after I use 'ui->tableWidget->setRowCount(index);', and later set back to 100, the code "ui->tableWidget->item(index, 0)->setText(...);" will crash the program. That's so bad!!! ;(
I debugged and find the new index > index set as row count before 'setting back to 100'.
Did the system delete the table items automatically when I set smaller row count???
I fear about this so much because even my code cannot determine the lifetime of the objects I created... Does anyone know how to keep them 'alive' after setting row count?(otherwise, I have to new them...).
I really appreciate it you take the patience to read my poor ELis:)
new:
//TABLE
ui->tableWidget->setColumnCount(3);
ui->tableWidget->setRowCount(100);
ui->tableWidget->setHorizontalHeaderLabels(headers);
for(int i = 0; i < 100; i++)//new
{
ui->tableWidget->setItem( i, 0 , new QTableWidgetItem(""));//time
ui->tableWidget->setItem( i, 1 , new QTableWidgetItem(""));//name
ui->tableWidget->setItem( i, 2 , new QTableWidgetItem(""));//BITS
}
Related code lines only:
{
int index = 0;
for(int queue_i = size_1; queue_i >= 0; queue_i--)
{
if(logDisplayQueue.at(queue_i).at(3) == "0" || logDisplayQueue.at(queue_i).at(3) == "2")continue;
QStringList BITList = bits2Hexs(queue_i);
ui->tableWidget->item(index, 0)->setText(logDisplayQueue.at(queue_i).at(0));//time
ui->tableWidget->item(index, 1)->setText(logDisplayQueue.at(queue_i).at(1));//name
ui->tableWidget->item(index, 2)->setText(BITList.join(""));//BITS
if(queue_i == oldRowItemNo)ui->tableWidget->selectRow(index);
index++;
}
ui->tableWidget->setRowCount(index);//set row count to be 30 more or less
}
Another function:
{
ui->tableWidget->setRowCount(100);//back to be 100 again
for(int queue_i = size_1, index = 0; queue_i >= 0; queue_i--, index++)
{
QStringList BITList = bits2Hexs(queue_i);
ui->tableWidget->item(index, 0)->setText(logDisplayQueue.at(queue_i).at(0));//time
ui->tableWidget->item(index, 1)->setText(logDisplayQueue.at(queue_i).at(1));//name
ui->tableWidget->item(index, 2)->setText(BITList.join(""));//BITS
//In debugging, when index reches the value of old row-count, "->setText" crashes the //program.
if(queue_i == oldRowItemNo)ui->tableWidget->selectRow(index);
}
}
When running, it returns message like 'instruction 0x00421727 refers to 0x00000000 memory, the memory cannot be 'read''
if I comment off this line:
'ui->tableWidget->setRowCount(index);//set row count to be 30 more or less', it runs well without crash and rows after index-referred-row show the same data as before.
setRowCount ensures that the table holds exactly that many rows. If you had more rows than index before, those rows are gone (deleted).
If you want to temporarily hide rows, you should probably use hideRow(int)/showRow rather than resetting the row count.