QT19 How to link QComboBox with sqlite Database values

前端之家收集整理的这篇文章主要介绍了QT19 How to link QComboBox with sqlite Database values前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.modify employeeinfo window


2.modify employeeinfo.cpp

#include "employeeinfo.h"
#include "ui_employeeinfo.h"
#include "login.h"
#include <QMessageBox>
EmployeeInfo::EmployeeInfo(QWidget *parent) :
    QDialog(parent),ui(new Ui::EmployeeInfo)
{
    ui->setupUi(this);
    if(!login::connOpen())
    {
        ui->label_sec_status->setText("Failed to open the database") ;

    }
    else
    {
        ui->label_sec_status->setText("Connected...") ;

    }
}

EmployeeInfo::~EmployeeInfo()
{
    delete ui;
}

void EmployeeInfo::on_pushButton_clicked()
{

    QString eid,name,surname,age;
    eid = ui->txt_eid->text();
    name =ui->txt_name->text();
    surname = ui->txt_surname->text();
    age = ui->txt_age->text();

    if(!login::connOpen())
    {
        qDebug() << "Failed to open the database";
        return ;
    }

    QsqlQuery qry;

    QString stmt = "insert into employeeinfo (eid,age) values('"+ eid +"','"+ name +"','"+ surname +"','"+ age +"')";
    qry.prepare(stmt);
    if( qry.exec())
    {
        QMessageBox::information(this,tr("Save"),tr("Saved"));
        login::connClose();
    }
    else
    {
        QMessageBox::critical(this,tr("Error"),qry.lastError().text());
    }
}

void EmployeeInfo::on_pushButton_edit_clicked()
{
    QString eid,age;
    eid = ui->txt_eid->text();
    name =ui->txt_name->text();
    surname = ui->txt_surname->text();
    age = ui->txt_age->text();

    if(!login::connOpen())
    {
        qDebug() << "Failed to open the database";
        return ;
    }

    QsqlQuery qry;

    QString stmt = "update employeeinfo set eid='"+ eid+"',name='"+ name+ "',surname='"+ surname+"',age='"+ age +"'where eid='" + eid + "'";
    qry.prepare(stmt);
    if( qry.exec())
    {
        QMessageBox::information(this,tr("Edit"),tr("Updated"));
        login::connClose();
    }
    else
    {
        QMessageBox::critical(this,qry.lastError().text());
    }
}

void EmployeeInfo::on_pushButton_delete_clicked()
{
    QString eid,age;
    eid = ui->txt_eid->text();
//    name =ui->txt_name->text();
//    surname = ui->txt_surname->text();
//    age = ui->txt_age->text();

    if(!login::connOpen())
    {
        qDebug() << "Failed to open the database";
        return ;
    }

    QsqlQuery qry;

    QString stmt = "delete from employeeinfo where eid = '"+ eid +"'";
    qry.prepare(stmt);
    if( qry.exec())
    {
        QMessageBox::information(this,tr("Delete"),tr("Deleted"));
        login::connClose();
    }
    else
    {
        QMessageBox::critical(this,qry.lastError().text());
    }
}

void EmployeeInfo::on_pushButton_load_clicked()
{
    QsqlQueryModel * modal = new QsqlQueryModel();
    login::connOpen();
    QsqlQuery* qry = new QsqlQuery(login::mydb);
    QString stmt;

    stmt = "select eid,surname from employeeinfo";

    qry->prepare(stmt);
    qry->exec();
    modal->setQuery(*qry);
    ui->tableView->setModel(modal);

    login::connClose();
    qDebug() << modal->rowCount();
}

void EmployeeInfo::on_pushButton_load_list_clicked()
{
    QsqlQueryModel * modal = new QsqlQueryModel();
    login::connOpen();
    QsqlQuery* qry = new QsqlQuery(login::mydb);
    QString stmt;

    stmt = "select name from employeeinfo";

    qry->prepare(stmt);
    qry->exec();
    modal->setQuery(*qry);
    ui->listView->setModel(modal);

    login::connClose();
    qDebug() << modal->rowCount();
}

void EmployeeInfo::on_pushButton_load_combo_clicked()
{
    QsqlQueryModel * modal = new QsqlQueryModel();
    login::connOpen();
    QsqlQuery* qry = new QsqlQuery(login::mydb);
    QString stmt;

    stmt = "select name from employeeinfo";

    qry->prepare(stmt);
    qry->exec();
    modal->setQuery(*qry);
    ui->comboBox->setModel(modal);

    login::connClose();
    qDebug() << modal->rowCount();
}
原文链接:https://www.f2er.com/sqlite/198975.html

猜你在找的Sqlite相关文章