1
新建一个项目:
Database01.pro |
SOURCES main.cpp Contact.cpp QT CONFIG HEADERS Contact.h |
Contact.h |
#ifndef CONTACT_H #define CONTACT_H #include <QWidget> #include <QSqlTableModel> #include <QTableView> #include <QLineEdit> #include <QPushButton> class Contact : public QWidget { Q_OBJECT public: explicit Contact(QWidget *parent); QSqlTableModel* _model; QTableView* _view; QLineEdit* _filter; QPushButton* _add; QPushButton* _del; QPushButton* _reset; QPushButton* _submit; signals: public slots: void slotModelDataChanged(QModelIndex,QModelIndex); void slotFilterChanged(QString filter); }; #endif // CONTACT_H |
Contact.cpp |
#include "Contact.h" #include <QVBoxLayout> #include <QHBoxLayout> #include <QSqlRecord> #include <QCompleter> #include <QDebug> Contact::Contact(QWidget *parent) : QWidget(parent) { //创建一个QSqlTableModel _model = new QSqlTableModel; //创建QTable _view = new QTableView; //view里面设置model _view->setModel(_model); _model->setTable("tcontact"); //手动提交 _model->setEditStrategy(QSqlTableModel::OnManualSubmit); connect(_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(slotModelDataChanged(QModelIndex,QModelIndex))); _model->select(); // set Layout QVBoxLayout* vBox = new QVBoxLayout(this); vBox->addWidget(_view); QHBoxLayout* hBox = new QHBoxLayout; vBox->addLayout(hBox); //添加add hBox->addWidget(_filter = new QLineEdit, 1); hBox->addWidget(_add=new QPushButton("Add")); hBox->addWidget(_del=new QPushButton("Del")); hBox->addWidget(_reset=new QPushButton("Reset")); hBox->addWidget(_submit=new QPushButton("Submit")); connect(_add, &QPushButton::clicked, [&](){ QSqlRecord record = _model->record(); _model->insertRecord(-1, record); }); connect(_del, &QPushButton::clicked, [&](){}); connect(_reset, &QPushButton::clicked, [&](){}); connect(_submit, &QPushButton::clicked, [&](){ _model->submitAll(); }); //模糊查询 connect(_filter, SIGNAL(textChanged(QString)), this, SLOT(slotFilterChanged(QString))); slotModelDataChanged(QModelIndex(), QModelIndex()); } void Contact::slotFilterChanged(QString filter) { if(filter.isEmpty()) { _model->setFilter(""); _model->select(); return; } // username like filter or password like filter ....... QSqlRecord record = _model->record(); QString modelFilter; for(int i=0; i<record.count(); ++i) { if(i!=0) { modelFilter += " or "; } QString field = record.fieldName(i); QString subFilter = QString().sprintf("%s like '%%%s%%'", field.toUtf8().data(), filter.toUtf8().data()); // qDebug() << subFilter; modelFilter += subFilter; } qDebug() << modelFilter; _model->setFilter(modelFilter); _model->select(); } void Contact::slotModelDataChanged(QModelIndex,QModelIndex) { QStringList strList; for(int i=0; i<_model->rowCount(); ++i) { QSqlRecord record = _model->record(i); for(int j=0; j<record.count(); ++j) { QVariant var = record.value(j); if(var.isNull()) continue; strList << var.toString(); } } qDebug() << strList; QCompleter* completer=new QCompleter(strList); _filter->setCompleter(completer); } |
main.cpp |
#include <QApplication> #include "Widget05.h" #include <QSqlDatabase> #include <QSqlError> #include <QDebug> #include "Contact.h" int main(int argc,char* argv[]) { QApplication app(argc,argv); /*QT可以操作 QSLITE QODBC,QPLSQL 这些数据库*/ //下面表示使用mysql数据库,因为这里的db没有用到db,所以可以把它放在main中 //本质:在QT里面打开一个数据库之后,就会保存一个数据库连接, //其它的位置就可以任意使用这个全局的变量了 QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("127.0.0.1"); //设置数据库所在位置 db.setUserName("root"); //设置数据库的用户名 db.setPassword("123456"); //设置数据库的密码 db.setDatabaseName("d0718"); //设置数据库名称 bool bRet = db.open(); //打开数据库连接 if(bRet == false) { //说明可以通过db.lastError()的方式得到错误信息 qDebug() << "error open database" << db.lastError().text(); exit(0); } qDebug() << "open database success"; //注意Widget02要写在上面代码的下面 Contact c; c.show(); return app.exec(); } |
运行结果: |