|
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <QLineEdit>
#include <QtSql>
#include <QString>
class shiyan : public QWidget
{
QPushButton *charu;
QPushButton *chaxun;
QLineEdit *bianji;
QSqlDatabase db;
QSqlQuery query;
public:
shiyan(QWidget *parent = 0);
public slots:
void charuzhi(void);
void huoquzhi(void);
};
shiyan::shiyan(QWidget *parent)
: QWidget(parent)
{
charu = new QPushButton("charu");
chaxun = new QPushButton("chaxun");
bianji = new QLineEdit();
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(bianji);
layout->addWidget(charu);
layout->addWidget(chaxun);
setLayout(layout);
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("zzh");
db.open();
connect(charu, SIGNAL(clicked()),bianji, SLOT(charuzhi()));
connect(chaxun, SIGNAL(clicked()),bianji, SLOT(huoquzhi()));
}
void shiyan::charuzhi(void)
{
QString zhi = bianji->text();
query.prepare("insert into biao values(?)");
query.addBindValue(zhi);
}
void shiyan::huoquzhi(void)
{
query.exec("SELECT a FROM biao");
while (query.next()) {
QString name = query.value(0).toString();
bianji->setText(name);
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
shiyan widget;
widget.show();
return app.exec();
}
输入信息后,一个槽将信息存在数据库里,另一个槽是在数据库里读取信息并显示。 |
|