Not capturing hidden rows in QTableWidget
-
Hi,
I have a method that looks at a QTableWidget and copies its contents to a clipboard which I can use later for a c/p function. The method works fine but if I hide certain rows in the table, the method still reads them and copies them to the clipboard. Below is the code. Would anyone know how I can check if a row is hidden and not copy it to the clipboard?Thank you!
void copyTextFromTableWidgetToClipBoard()
{
QAbstractItemModel *tableModel = tableWidget_->model();
QItemSelectionModel *selectionModel = tableWidget_->selectionModel();
QModelIndexList selectedCells = selectionModel->selectedIndexes();std::sort(selectedCells.begin(), selectedCells.end()); if(selectedCells.size() < 1) return; QString copy_table; QModelIndex previous = selectedCells.first(); selectedCells.removeFirst(); for (int cell = 0; cell < selectedCells.size(); cell++) { QVariant data = tableModel->data(previous); QString text = data.toString(); QModelIndex index = selectedCells.at(cell); copy_table.append(text); if(index.row() != previous.row()) { copy_table.append('\n'); } else { copy_table.append('\t'); } previous = index; } copy_table.append(tableModel->data(selectedCells.last()).toString()); copy_table.append('\n'); QClipboard *clipboard = QApplication::clipboard(); clipboard->setText(copy_table);
}
-
@leinad
And how do you hide the rows? QTableView::setRowHidden()? Which has a corresponding QTableView::isRowHidden()?If you have selected cells, as per your code, I have no idea but did you test whether any hidden cells are even included in those selected? I think I would have expected not.
-
@leinad
bool QTableView::isIndexHidden(const QModelIndex &index)?
bool QTableView::isRowHidden(int row) passingindex.row()
?But did you verify whether your
selectedCells()
actually includes hidden rows/cells anyway? -
Yes, selected cells do include hidden rows which I was unaware that it did.
I was going to try this:
QModelIndex rowNumber;
QModelIndexList selectedCells = selectionModel->selectedIndexes();
for (int cell = 0; cell < selectedCells.size(); cell++)
{
rowNumber = selectedCells[cell];
if(ui->tableWidget->isRowHidden(rowNumber.row()))
continue;
} -
@leinad said in Not capturing hidden rows in QTableWidget:
You are addressing the question with QTableView but I'm using QTableWidget.
Inherits: QTableView
And in any case I previously asked you:
And how do you hide the rows? QTableView::setRowHidden()? Which has a corresponding QTableView::isRowHidden()?
So why not answer that? Together with
But did you verify whether your selectedCells() actually includes hidden rows/cells anyway?
-
@leinad
But if you go to the QTableWidget Class page it does not have its ownsetRowHidden()
, does it? It inherits QTableView::setRowHidden().In any case the answers are above.
-
@leinad Just to verify, print out the contents of the row when you hide it, and when you skip it.
here:
@leinad said in Not capturing hidden rows in QTableWidget:ui->tableWidget->setRowHidden( row, true );
and here:
@leinad said in Not capturing hidden rows in QTableWidget:
if(ui->tableWidget->isRowHidden(rowNumber.row()))
continue;
}Just to see if it's hiding and skipping the correct rows.
-