Created
May 29, 2019 08:32
-
-
Save MultiMote/d9f5f2979b9625fd9b840ab9cff3b9f7 to your computer and use it in GitHub Desktop.
Copy QTableView selection to clipboard (ignore custom column order)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void copyTableSelectionToClipboard(QTableView *view) { | |
if (!view->model()) { | |
return; | |
} | |
int min_col = view->model()->columnCount() - 1; | |
int max_col = 0; | |
int min_row = view->model()->rowCount() - 1; | |
int max_row = 0; | |
QString text; | |
for (auto &sel : view->selectionModel()->selection()) { | |
min_col = qMin(min_col, sel.left()); | |
max_col = qMax(max_col, sel.right()); | |
min_row = qMin(min_row, sel.top()); | |
max_row = qMax(max_row, sel.bottom()); | |
} | |
for (int row = min_row; row <= max_row; ++row) { | |
for (int col = min_col; col <= max_col; ++col) { | |
text += view->model()->index(row, col).data().toString(); | |
if (col != max_col) { | |
text += '\t'; | |
} | |
} | |
if (row != max_row) { | |
text += '\n'; | |
} | |
} | |
QApplication::clipboard()->setText(text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment