-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
70 lines (57 loc) · 2.35 KB
/
Copy pathmainwindow.cpp
File metadata and controls
70 lines (57 loc) · 2.35 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
67
68
69
70
#include <QtWidgets>
#include <QtSql>
#include "mainwindow.h"
DbView::DbView(const QStringList &tableNames, QWidget *parent)
: QWidget(parent)
{
this->setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
this->setFixedSize(sizeHint());
QLabel* dbName = new QLabel;
QComboBox *tablesBox = new QComboBox;
tablesBox->addItems(tableNames);
model = new QSqlTableModel(this);
model->setTable(tablesBox->currentText());
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
dbName->setText("Database name: " + model->database().databaseName());
QTableView *view = new QTableView;
view->setModel(model);
view->resizeColumnsToContents();
view->setFixedSize(view->sizeHint());
submitButton = new QPushButton(tr("Submit"), this);
submitButton->setDefault(true);
revertButton = new QPushButton(tr("&Revert"), this);
quitButton = new QPushButton(tr("Quit"), this);
buttonBox = new QDialogButtonBox(Qt::Horizontal);
buttonBox->addButton(submitButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(revertButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
connect(tablesBox, &QComboBox::currentTextChanged, this, [this, tablesBox]()-> void {
model->setTable(tablesBox->currentText());
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
});
connect(submitButton, &QPushButton::clicked, this, &DbView::submit);
connect(revertButton, &QPushButton::clicked, model, &QSqlTableModel::revertAll);
connect(quitButton, &QPushButton::clicked, this, &DbView::close);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->setSizeConstraint(QLayout::SizeConstraint::SetDefaultConstraint);
mainLayout->addWidget(dbName);
mainLayout->addWidget(tablesBox);
mainLayout->addWidget(view);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("DbEasy"));
}
void DbView::submit()
{
model->database().transaction();
if (model->submitAll()) {
model->database().commit();
} else {
model->database().rollback();
QMessageBox::warning(this, tr("DbEasy"),
tr("The database reported an error: %1")
.arg(model->lastError().text()));
}
}