Added generic copySelectionToClipboard function that takes the selection

of a model based tableview and copies the data as CSV to the clipoard.
This commit is contained in:
Eelke Klein 2017-02-04 11:54:18 +01:00
parent 798f8f51b9
commit 242f5464df
2 changed files with 36 additions and 0 deletions

View file

@ -1,4 +1,8 @@
#include "util.h" #include "util.h"
#include "csvwriter.h"
#include <QApplication>
#include <QTextStream>
#include <QClipboard>
// Supported range from microseconds to seconds // Supported range from microseconds to seconds
// min:sec to hours::min::sec // min:sec to hours::min::sec
@ -47,3 +51,33 @@ QString msfloatToHumanReadableString(float ms)
QString result = QString::asprintf("%0.*f", deci, val); QString result = QString::asprintf("%0.*f", deci, val);
return result + unit; return result + unit;
} }
void copySelectionToClipboard(const QTableView *view)
{
//QAbstractItemModel * model = resultModel; //view->model();
QItemSelectionModel * selection = view->selectionModel();
QModelIndexList selectedIndexes = selection->selectedIndexes();
if (selectedIndexes.count() > 0) {
QString clipboard_string;
QTextStream out(&clipboard_string, QIODevice::WriteOnly);
CsvWriter csv(&out);
csv.setSeperator('\t');
csv.setQuote('"');
int previous_row = selectedIndexes[0].row();
// for (int i = 0; i < selectedIndexes.count(); ++i) {
// QModelIndex current = selectedIndexes[i];
for (auto current : selectedIndexes) {
if (current.row() > previous_row) {
csv.nextRow();
previous_row = current.row();
}
QString display_text = current.data(Qt::DisplayRole).toString();
csv.writeField(display_text);
}
out.flush();
QApplication::clipboard()->setText(clipboard_string);
}
}

2
util.h
View file

@ -2,7 +2,9 @@
#define UTIL_H #define UTIL_H
#include <QString> #include <QString>
#include <QTableView>
QString msfloatToHumanReadableString(float ms); QString msfloatToHumanReadableString(float ms);
void copySelectionToClipboard(const QTableView *view);
#endif // UTIL_H #endif // UTIL_H