1. 程式人生 > >3.QT資料庫綜合案例,模糊查詢等操作

3.QT資料庫綜合案例,模糊查詢等操作



1 新建一個專案:

Database01.pro

SOURCES+=\

main.cpp\

Contact.cpp

QT+=guiwidgets sql

CONFIG+=C++11

HEADERS+=\

Contact.h

Contact.h

#ifndefCONTACT_H
#defineCONTACT_H
#include<QWidget>
#include<QSqlTableModel>
#include<QTableView>
#include<QLineEdit>
#include
<QPushButton>
classContact:publicQWidget
{
Q_OBJECT
public:
explicitContact(QWidget*parent=0);
QSqlTableModel*_model;
QTableView*_view;
QLineEdit*_filter;
QPushButton*_add;
QPushButton*_del;
QPushButton*_reset;
QPushButton*_submit;
signals:
publicslots:
voidslotModelDataChanged(QModelIndex,
QModelIndex);
voidslotFilterChanged(QStringfilter);
};
#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=newQSqlTableModel;
//建立QTable
_view=newQTableView;
//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();
//setLayout
QVBoxLayout*vBox=newQVBoxLayout(this);
vBox->addWidget(_view);
QHBoxLayout*hBox=newQHBoxLayout;
vBox->addLayout(hBox);
//新增add
hBox->addWidget(_filter=newQLineEdit,1);
hBox->addWidget(_add=newQPushButton("Add"));
hBox->addWidget(_del=newQPushButton("Del"));
hBox->addWidget(_reset=newQPushButton("Reset"));
hBox->addWidget(_submit=newQPushButton("Submit"));
connect(_add,&QPushButton::clicked,[&](){
QSqlRecordrecord=_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());
}
voidContact::slotFilterChanged(QStringfilter)
{
if(filter.isEmpty())
{
_model->setFilter("");
_model->select();
return;
}
//usernamelikefilterorpasswordlikefilter.......
QSqlRecordrecord=_model->record();
QStringmodelFilter;
for(inti=0;i<record.count();++i)
{
if(i!=0)
{
modelFilter+="or";
}
QStringfield=record.fieldName(i);
QStringsubFilter=QString().sprintf("%slike'%%%s%%'",field.toUtf8().data(),filter.toUtf8().data());
//qDebug()<<subFilter;
modelFilter+=subFilter;
}
qDebug()<<modelFilter;
_model->setFilter(modelFilter);
_model->select();
}
voidContact::slotModelDataChanged(QModelIndex,QModelIndex)
{
QStringListstrList;
for(inti=0;i<_model->rowCount();++i)
{
QSqlRecordrecord=_model->record(i);
for(intj=0;j<record.count();++j)
{
QVariantvar=record.value(j);
if(var.isNull())continue;
strList<<var.toString();
}
}
qDebug()<<strList;
QCompleter*completer=newQCompleter(strList);
_filter->setCompleter(completer);
}

main.cpp

#include<QApplication>
#include"Widget05.h"
#include<QSqlDatabase>
#include<QSqlError>
#include<QDebug>
#include"Contact.h"
intmain(intargc,char*argv[])
{
QApplicationapp(argc,argv);
/*QT可以操作QSLITEQODBC,QPLSQL這些資料庫*/
//下面表示使用mysql資料庫,因為這裡的db沒有用到db,所以可以把它放在main
//本質:在QT裡面開啟一個數據庫之後,就會儲存一個數據庫連線,
//其它的位置就可以任意使用這個全域性的變量了
QSqlDatabasedb=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");//設定資料庫所在位置
db.setUserName("root");//設定資料庫的使用者名稱
db.setPassword("123456");//設定資料庫的密碼
db.setDatabaseName("d0718");//設定資料庫名稱
boolbRet=db.open();//開啟資料庫連線
if(bRet==false)
{
//說明可以通過db.lastError()的方式得到錯誤資訊
qDebug()<<"erroropendatabase"<<db.lastError().text();
exit(0);
}
qDebug()<<"opendatabasesuccess";
//注意Widget02要寫在上面程式碼的下面
Contactc;
c.show();
returnapp.exec();
}

執行結果: