refactor: extract record count refresh into updateCount

This commit is contained in:
Andrey Kutin 2026-10-23 21:09:09 +03:00
parent 1435f78530
commit 5abef64c69
2 changed files with 14 additions and 11 deletions

View File

@ -31,17 +31,21 @@ void Widget::init(const QSqlDatabase &db)
close(); close();
return; return;
} }
QSqlQuery query(_db); updateCount();
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);
}
}
show(); 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() void Widget::addRecord()
{ {
if (_nameEdit->displayText().isEmpty() || _textEdit->toPlainText().isEmpty()) { if (_nameEdit->displayText().isEmpty() || _textEdit->toPlainText().isEmpty()) {
@ -60,6 +64,5 @@ void Widget::addRecord()
} }
_nameEdit->clear(); _nameEdit->clear();
_textEdit->clear(); _textEdit->clear();
++count; updateCount();
_countId->setText(QString::number(count, 10));
} }

View File

@ -26,7 +26,7 @@ private:
QLabel *_textLabel = nullptr; QLabel *_textLabel = nullptr;
QTextEdit *_textEdit = nullptr; QTextEdit *_textEdit = nullptr;
QPushButton *_addBtn = nullptr; QPushButton *_addBtn = nullptr;
quint64 count = 0; void updateCount();
public slots: public slots:
void init(const QSqlDatabase &); void init(const QSqlDatabase &);
void addRecord(); void addRecord();