refactor: extract record count refresh into updateCount
This commit is contained in:
parent
1435f78530
commit
5abef64c69
23
widget.cpp
23
widget.cpp
|
|
@ -31,17 +31,21 @@ void Widget::init(const QSqlDatabase &db)
|
|||
close();
|
||||
return;
|
||||
}
|
||||
QSqlQuery query(_db);
|
||||
if (query.exec("SELECT COUNT(*) FROM random_data")) {
|
||||
auto countRecord = query.value(0).toString();
|
||||
count = query.value(0).toInt();
|
||||
if (!countRecord.isEmpty()) {
|
||||
_countId->setText(countRecord);
|
||||
}
|
||||
}
|
||||
updateCount();
|
||||
show();
|
||||
}
|
||||
|
||||
void Widget::updateCount()
|
||||
{
|
||||
if (!_db.isOpen()) {
|
||||
return;
|
||||
}
|
||||
QSqlQuery query(_db);
|
||||
if (query.exec("SELECT COUNT(*) FROM random_data") && query.next()) {
|
||||
_countId->setText(query.value(0).toString());
|
||||
}
|
||||
}
|
||||
|
||||
void Widget::addRecord()
|
||||
{
|
||||
if (_nameEdit->displayText().isEmpty() || _textEdit->toPlainText().isEmpty()) {
|
||||
|
|
@ -60,6 +64,5 @@ void Widget::addRecord()
|
|||
}
|
||||
_nameEdit->clear();
|
||||
_textEdit->clear();
|
||||
++count;
|
||||
_countId->setText(QString::number(count, 10));
|
||||
updateCount();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue