add: widget form to insert records into random_data
This commit is contained in:
parent
be51020e75
commit
1435f78530
62
widget.cpp
62
widget.cpp
|
|
@ -1,5 +1,65 @@
|
|||
#include "widget.h"
|
||||
#include <QSqlQuery>
|
||||
|
||||
Widget::Widget (QWidget *parent) : QWidget (parent) {}
|
||||
Widget::Widget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, _grid(new QGridLayout(this))
|
||||
, _countId(new QLabel("NULL"))
|
||||
, _nameLabel(new QLabel("name: "))
|
||||
, _nameEdit(new QLineEdit())
|
||||
, _textLabel(new QLabel("text:"))
|
||||
, _textEdit(new QTextEdit())
|
||||
, _addBtn(new QPushButton("&ADD"))
|
||||
{
|
||||
_grid->addWidget(_countId, 0, 0, Qt::AlignHCenter);
|
||||
_grid->addWidget(_nameLabel, 1, 0, Qt::AlignRight);
|
||||
_grid->addWidget(_nameEdit, 1, 1, Qt::AlignVCenter);
|
||||
_grid->addWidget(_textLabel, 2, 0, Qt::AlignRight);
|
||||
_grid->addWidget(_textEdit, 2, 1, Qt::AlignHCenter);
|
||||
_grid->addWidget(_addBtn, 3, 0, Qt::AlignHCenter);
|
||||
setLayout(_grid);
|
||||
|
||||
connect(_addBtn, &QPushButton::clicked, this, &Widget::addRecord);
|
||||
}
|
||||
|
||||
Widget::~Widget () {}
|
||||
|
||||
void Widget::init(const QSqlDatabase &db)
|
||||
{
|
||||
_db = db;
|
||||
if (!_db.isOpen()) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
show();
|
||||
}
|
||||
|
||||
void Widget::addRecord()
|
||||
{
|
||||
if (_nameEdit->displayText().isEmpty() || _textEdit->toPlainText().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (!_db.isOpen()) {
|
||||
return;
|
||||
}
|
||||
QSqlQuery query(_db);
|
||||
query.prepare("INSERT INTO public.random_data (name, text) "
|
||||
"VALUES (?, ?)");
|
||||
query.addBindValue(_nameEdit->displayText());
|
||||
query.addBindValue(_textEdit->toHtml());
|
||||
if (!query.exec()) {
|
||||
return;
|
||||
}
|
||||
_nameEdit->clear();
|
||||
_textEdit->clear();
|
||||
++count;
|
||||
_countId->setText(QString::number(count, 10));
|
||||
}
|
||||
|
|
|
|||
24
widget.h
24
widget.h
|
|
@ -1,6 +1,12 @@
|
|||
#ifndef WIDGET_H
|
||||
#define WIDGET_H
|
||||
|
||||
#include <QGridLayout>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QSqlDatabase>
|
||||
#include <QTextEdit>
|
||||
#include <QWidget>
|
||||
|
||||
class Widget : public QWidget
|
||||
|
|
@ -8,7 +14,21 @@ class Widget : public QWidget
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Widget (QWidget *parent = nullptr);
|
||||
~Widget ();
|
||||
Widget(QWidget *parent = nullptr);
|
||||
~Widget();
|
||||
|
||||
private:
|
||||
QSqlDatabase _db;
|
||||
QGridLayout *_grid = nullptr;
|
||||
QLabel *_countId = nullptr;
|
||||
QLabel *_nameLabel = nullptr;
|
||||
QLineEdit *_nameEdit = nullptr;
|
||||
QLabel *_textLabel = nullptr;
|
||||
QTextEdit *_textEdit = nullptr;
|
||||
QPushButton *_addBtn = nullptr;
|
||||
quint64 count = 0;
|
||||
public slots:
|
||||
void init(const QSqlDatabase &);
|
||||
void addRecord();
|
||||
};
|
||||
#endif // WIDGET_H
|
||||
|
|
|
|||
Loading…
Reference in New Issue