-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQTableWidgetWithCopy.cpp
More file actions
53 lines (45 loc) · 1.68 KB
/
QTableWidgetWithCopy.cpp
File metadata and controls
53 lines (45 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <QApplication>
#include <QClipboard>
#include <QKeyEvent>
#include "QTableWidgetWithCopy.h"
#include <algorithm>
void QTableWidgetWithCopy::copy() {
QItemSelectionModel * selection = selectionModel();
QModelIndexList indexes = selection->selectedIndexes();
if(indexes.size() < 1)
return;
// QModelIndex::operator < sorts first by row, then by column.
// this is what we need
std::sort(indexes.begin(), indexes.end());
// You need a pair of indexes to find the row changes
QModelIndex previous = indexes.first();
indexes.removeFirst();
QString selected_text;
QModelIndex current;
Q_FOREACH(current, indexes) {
QVariant data = model()->data(previous);
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(QLatin1Char('\n'));
} else {
// Otherwise it's the same row, so append a column separator, which is a tab.
selected_text.append(QLatin1Char('\t'));
}
previous = current;
}
// add last element
selected_text.append(model()->data(current).toString());
selected_text.append(QLatin1Char('\n'));
QApplication::clipboard()->setText(selected_text);
}
void QTableWidgetWithCopy::keyPressEvent(QKeyEvent * event) {
if(event->matches(QKeySequence::Copy) ) {
copy();
} else {
QTableWidget::keyPressEvent(event);
}
}