一起看代码来玩玩QT之18 database(five 用lineEidt 对database 进行高级别的查询,使QTableView显示所需的数据)


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 = 0);

    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)
{
    _model = new QSqlTableModel;
    _view = new QTableView;
    _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);

    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 <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include "Contact.h"

int main(int argc, char*argv[])
{
    QApplication app(argc, argv);

    /* QSQLITE QODBC QPLSQL */
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");
    db.setUserName("root");
    db.setPassword("iamxgl");
    db.setDatabaseName("d0718");
    bool bRet = db.open();
    if(bRet == false)
    {
        qDebug() << "error open database" << db.lastError().text();
        exit(0);
    }
    qDebug() << "open database success";

    Contact w;
    w.show();
    return app.exec();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值