您的位置:首页 > 编程语言 > Qt开发

3.QT数据库综合案例,模糊查询等操作

2014-12-16 20:41 639 查看


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();

}

运行结果:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐