加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

QT18 how to link QListView with sqlite Database values

发布时间:2020-12-12 19:32:19 所属栏目:百科 来源:网络整理
导读:1. modify employeeinfo window 2. modify employeeinfo.cpp #include "employeeinfo.h"#include "ui_employeeinfo.h"#include "login.h"#include QMessageBoxEmployeeInfo::EmployeeInfo(QWidget *parent) : QDialog(parent),ui(new Ui::EmployeeInfo){ ui-

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


3.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读