To actually capture the selection you use the item view's selection model to get a list of indices. Given that you have a QTableView *
called view
you get the selection this way:
QAbstractItemModel * model = view->model();
QItemSelectionModel * selection = view->selectionModel();
QModelIndexList indexes = selection->selectedIndexes();
Then loop through the index list calling model->data(index)
on each index. Convert the data to a string if it isn't already and concatenate each string together. Then you can use QClipboard.setText
to paste the result to the clipboard. Note that, for Excel and Calc, each column is separated from the next by a newline ("
") and each row is separated by a tab (""). You have to check the indices to determine when you move to the next row.
QString selected_text;
// You need a pair of indexes to find the row changes
QModelIndex previous = indexes.first();
indexes.removeFirst();
foreach(const QModelIndex ¤t, indexes)
{
QVariant data = model->data(current);
QString text = data.toString();
// At this point `text` contains the text in one cell
selected_text.append(text);
// If you are at the start of the row the row number of the previous index
// isn't the same. Text is followed by a row separator, which is a newline.
if (current.row() != previous.row())
{
selected_text.append('
');
}
// Otherwise it's the same row, so append a column separator, which is a tab.
else
{
selected_text.append('');
}
previous = current;
}
QApplication.clipboard().setText(selected_text);
Warning: I have not had a chance to try this code, but a PyQt equivalent works.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…