I translate the answer from here :
To actually capture a selection, use a QItemSelectionModel for a list of indexes. Suppose there is a view pointer of type QTableView* , you can get a sample like this:
QAbstractItemModel * model = view->model(); QItemSelectionModel * selection = view->selectionModel(); QModelIndexList indexes = selection->selectedIndexes();
Then go through the list of indexes, calling model->data(index) for each index. Convert the data to a string if it is not yet a string, and add all the strings together. Then use QClipboard.setText to place the text on the clipboard. Note that for Excel and Calc, each column must be separated from the next by a tab ( "\t" ), and each line is separated by a newline ( "\n" ). You need to look at the index when going to the next line.
QString selected_text; // You need a pair of indexes to find the row changes QModelIndex previous = indexes.first(); indexes.removeFirst(); foreach(current, 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('\n'); } // Otherwise it's the same row, so append a column separator, which is a tab. else { selected_text.append('\t'); } previous = current; } QApplication.clipboard().setText(selected_text);
NB: I did not check this code, but it works for PyQt.