I have QTableView with 100+ rows in it. But at a time only 6 rows are visible. To see next set of rows, I have to use scrool bar.
I want to print visible rows in QTableView. But could not do that. I could just able to print single selected row.
QItemSelectionModel *select = _table->selectionModel();
QModelIndexList selectedRow = select->selectedRows();
QModelIndex index = selectedRow.at(0);
int columnCount = 2; // there are 2 columns in a row
QString copySelectedRowText_;
for(int i = 0 ; i < columnCount; i++)
{
copySelectedRowText_ += index.sibling(index.row(), i).data().toString()+ " ";
}
qDebug() << copySelectedRowText_;
How to print visible rows in QTableView?
You can get the current line number through the value()
of the verticalScrollbar
, and you can also get the number of displayable lines through pagestep()
.
This is my code ,you can try it:
void TesWidget::onbtnClicked()
{
int start_index = ui.tableView->verticalScrollBar()->value();
int page_cnt = ui.tableView->verticalScrollBar()->pageStep();
int end_index = start_index + page_cnt;
int row_cnt = model_->rowCount();
int col_cnt = model_->columnCount();
QString text;
for (int i = start_index; i < row_cnt && i <= end_index; i++)
{
for (int j = 0; j < col_cnt; j++)
{
text.append(QStringLiteral("%1 ").arg(model_->item(i,j)->text()));
}
text.append("\n");
}
qDebug() << text;
}