Currently, I have a QTableView with an associated model (inheriting QAbstractTableModel). Through QTableView::customContextMenuRequested, I can copy the table like this :
void MyForm::onCopyClicked()
{
QItemSelectionModel *lpSelection = m_pUI->DATA_TABLE_VIEW->selectionModel();
QModelIndexList lIndices = lpSelection->selectedIndexes();
if(lIndices.isEmpty())
return;
QMap<int, bool> selectedColumnsMap;
foreach (QModelIndex current, lIndices) {
selectedColumnsMap[current.column()] = true;
}
QList<int> selectedColumns = selectedColumnsMap.uniqueKeys();
int minCol = selectedColumns.first();
// prepend headers for selected columnss
QString selectedText;
foreach (int column, selectedColumns) {
selectedText += m_pUI->DATA_TABLE_VIEW->model()->headerData(column, Qt::Horizontal, Qt::DisplayRole).toString();
if (column != selectedColumns.last())
selectedText += QLatin1Char('\t');
}
selectedText += QLatin1Char('\n');
// QModelIndex::operator < sorts first by row, then by column.
// this is what we need
std::sort(lIndices.begin(), lIndices.end());
int lastRow = lIndices.first().row();
int lastColumn = minCol;
foreach (QModelIndex current, lIndices) {
if (current.row() != lastRow) {
selectedText += QLatin1Char('\n');
lastColumn = minCol;
lastRow = current.row();
}
if (current.column() != lastColumn) {
for (int i = 0; i < current.column() - lastColumn; ++i)
selectedText += QLatin1Char('\t');
lastColumn = current.column();
}
const double lValue = m_pUI->DATA_LIST_VIEW->model()->data(current).toDouble();
selectedText += QString::number(lValue);
}
selectedText += QLatin1Char('\n');
QApplication::clipboard()->setText(selectedText);
}
The need is to be able to copy the selection to another application like Excel through the clipboard. This code works well in most cases.
But if I have a big table with something like 5 million cells, this code crash, the string becoming too big. I'd like to be able to copy lots of cells like it's possible on excel.
So I wonder what is the good way of doing this ?
Best would be with Qt, but it doesn't look possible, so maybe with another lib or even windows API ...