Added export of data in CSV format.
This commit is contained in:
parent
df866d7b67
commit
073c62048f
7 changed files with 56 additions and 3 deletions
|
|
@ -127,10 +127,14 @@ void MainWindow::on_actionSave_copy_of_SQL_as_triggered()
|
|||
|
||||
void MainWindow::on_actionExport_data_triggered()
|
||||
{
|
||||
QueryTab *tab = GetActiveQueryTab();
|
||||
if (tab) {
|
||||
QString home_dir = QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory);
|
||||
QString file_name = QFileDialog::getSaveFileName(this,
|
||||
tr("Export data"), home_dir, tr("CSV file (*.csv)"));
|
||||
|
||||
tab->exportData(file_name);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_actionClose_triggered()
|
||||
|
|
|
|||
10
querytab.cpp
10
querytab.cpp
|
|
@ -565,3 +565,13 @@ void QueryTab::copyQueryAsCString()
|
|||
QString cs = ConvertToMultiLineCString(command);
|
||||
QApplication::clipboard()->setText(cs);
|
||||
}
|
||||
|
||||
void QueryTab::exportData(const QString &file_name)
|
||||
{
|
||||
auto widget = ui->tabWidget->currentWidget();
|
||||
auto fi = std::find(resultList.begin(), resultList.end(), widget);
|
||||
if (fi != resultList.end()) {
|
||||
TuplesResultWidget* rw = *fi;
|
||||
rw->exportData(file_name);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ public:
|
|||
bool canClose();
|
||||
|
||||
void copyQueryAsCString();
|
||||
void exportData(const QString &filename);
|
||||
private:
|
||||
|
||||
// struct ResultTab {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@
|
|||
#include "ui_tuplesresultwidget.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
TuplesResultWidget::TuplesResultWidget(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::TuplesResultWidget)
|
||||
|
|
@ -24,3 +27,14 @@ void TuplesResultWidget::setResult(std::shared_ptr<QueryResultModel> res, float
|
|||
ui->lblRowCount->setText(rowcount_str);
|
||||
ui->lblElapsedTime->setText(msfloatToHumanReadableString(ms));
|
||||
}
|
||||
|
||||
void TuplesResultWidget::exportData(const QString &file_name) const
|
||||
{
|
||||
QFile file(file_name);
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
QTextStream out(&file);
|
||||
::exportTable(ui->ResultView, out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ public:
|
|||
~TuplesResultWidget();
|
||||
|
||||
void setResult(std::shared_ptr<QueryResultModel> res, float ms);
|
||||
void exportData(const QString &file_name) const;
|
||||
private:
|
||||
Ui::TuplesResultWidget *ui;
|
||||
|
||||
|
|
|
|||
22
util.cpp
22
util.cpp
|
|
@ -53,6 +53,28 @@ QString msfloatToHumanReadableString(float ms)
|
|||
return result + unit;
|
||||
}
|
||||
|
||||
void exportTable(const QTableView *view, QTextStream &out)
|
||||
{
|
||||
auto model = view->model();
|
||||
if (model) {
|
||||
CsvWriter csv(&out);
|
||||
csv.setSeperator('\t');
|
||||
csv.setQuote('"');
|
||||
|
||||
const int cols = model->columnCount();
|
||||
const int rows = model->rowCount();
|
||||
for (int row = 0; row < rows; ++row) {
|
||||
for (int col = 0; col < cols; ++col) {
|
||||
auto idx = model->index(row, col);
|
||||
auto display_text = idx.data(Qt::DisplayRole).toString();
|
||||
csv.writeField(display_text);
|
||||
}
|
||||
csv.nextRow();
|
||||
}
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
void copySelectionToClipboard(const QTableView *view)
|
||||
{
|
||||
//QAbstractItemModel * model = resultModel; //view->model();
|
||||
|
|
|
|||
1
util.h
1
util.h
|
|
@ -7,5 +7,6 @@
|
|||
QString msfloatToHumanReadableString(float ms);
|
||||
void copySelectionToClipboard(const QTableView *view);
|
||||
QString ConvertToMultiLineCString(const QString &in);
|
||||
void exportTable(const QTableView *view, QTextStream &out);
|
||||
|
||||
#endif // UTIL_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue