This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreviewwidget.cpp
72 lines (59 loc) · 2.03 KB
/
previewwidget.cpp
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
71
72
#include <QSqlQueryModel>
#include <QSqlQuery>
#include <QThread>
#include <QSqlError>
#include <QStringListModel>
#include "previewwidget.h"
#include "ui_previewwidget.h"
#include "utilities/bdatabasemanager.h"
PreviewWidget::PreviewWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::PreviewWidget),
model(new QSqlQueryModel(this)) {
ui->setupUi(this);
ui->resultTable->setModel(model);
connect(this, &PreviewWidget::count, this, &PreviewWidget::newTitle);
}
PreviewWidget::~PreviewWidget() {
delete ui;
}
void PreviewWidget::onQueryFinished() {
ui->resultTable->resizeColumnsToContents();
// ui->resultTable->setEnabled(true);
}
void PreviewWidget::onQueryStarted() {
// ui->resultTable->setEnabled(false);
}
void PreviewWidget::setBzEnabled(bool enabled) {
}
void PreviewWidget::release() {
model->clear();
model = new QSqlQueryModel(this);
ui->resultTable->setModel(model);
}
void PreviewWidget::updateBz(const QString &sqlText) {
const auto &queryText = sqlText;
model->removeRows(0, model->rowCount());
auto thread = QThread::create([this, queryText]() {
auto db = BDatabaseManager::readOnlyConnection("preview_thread");
QSqlQuery countQuery(db);
countQuery.exec("SELECT count(*) FROM bz");
countQuery.next();
emit count(countQuery.value(0).toInt());
model->setQuery(queryText, db);
model->query();
// while (model->canFetchMore()) model->fetchMore();
if (model->lastError().type() == QSqlError::NoError) {
emit statusBarMessage("Done", 3000);
} else {
emit statusBarMessage(QString("Query error: %1").
arg(model->lastError().databaseText()), 8000);
}
});
connect(thread, &QThread::started, this, &PreviewWidget::onQueryStarted);
connect(thread, &QThread::finished, this, &PreviewWidget::onQueryFinished);
thread->start();
}
void PreviewWidget::newTitle(int _count) {
ui->groupBox_2->setTitle(QString("All %1 Records").arg(_count));
}