-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExportSelectionDialog.cpp
More file actions
66 lines (53 loc) · 2 KB
/
ExportSelectionDialog.cpp
File metadata and controls
66 lines (53 loc) · 2 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
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "ExportSelectionDialog.h"
#include "ui_ExportSelectionDialog.h"
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
ExportSelectionDialog::ExportSelectionDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ExportSelectionDialog)
{
ui->setupUi(this);
ui->formatCombo->addItem("CSV");
}
ExportSelectionDialog::~ExportSelectionDialog()
{
delete ui;
}
void ExportSelectionDialog::init(QTableView *tableView)
{
m_tableView = tableView;
}
void ExportSelectionDialog::on_buttonBox_accepted()
{
// Show dialog and get filename
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::homePath(), tr("CSV Files (*.csv)"));
// If the user chose a file to save to
if (fileName.length()) {
// Create file handle
QFile file(fileName);
// If we could successfully open the file
if (file.open(QFile::WriteOnly | QFile::Truncate))
{
QTextStream data(&file);
QStringList strList;
// Output header
for (int i = 0; i < m_tableView->model()->columnCount(); i++) {
strList << "\"" + m_tableView->model()->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString() + "\" ";
}
data << strList.join(",") + "\n";
// Cycle through each row
for (int row = 0; row < m_tableView->model()->rowCount(); row++) {
strList.clear();
// Cycle through each column
for( int column = 0; column < m_tableView->model()->columnCount(); column++ ) {
QModelIndex index = m_tableView->model()->index(row, column, QModelIndex());
strList << "\"" + m_tableView->model()->data(index).toString() + "\" ";
}
data << strList.join( "," )+"\n";
}
// Close file handle
file.close();
}
}
}