QT20 database values in QLineEdit or textbox if select combobox

21 篇文章 0 订阅

1.modify employeeinfo window


2. modify employee.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,name,surname,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, 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 = "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, tr("Error"), qry.lastError().text());
    }
}

void EmployeeInfo::on_pushButton_delete_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 = "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, tr("Error"), 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 name from employeeinfo";

    qry->prepare(stmt);
    qry->exec();
    modal->setQuery(*qry);
    ui->tableView->setModel(modal);
    ui->listView->setModel(modal);
    ui->comboBox->setModel(modal);
    login::connClose();
    qDebug() << modal->rowCount();
}

void EmployeeInfo::on_comboBox_currentIndexChanged(const QString &arg1)
{
    QString name = ui->comboBox->currentText();
    if( !login::connOpen())
    {
        qDebug() << "Failed to open the database";
        return;
    }
    QSqlQuery qry;
    QString stsm = "select * from employeeinfo where name = '"+ name +"'";
    qry.prepare(stsm);
    if( qry.exec())
    {
        while(qry.next())
        {

            ui->txt_eid->setText(qry.value(0).toString());
            ui->txt_name->setText(qry.value(1).toString());
            ui->txt_surname->setText(qry.value(2).toString());
            ui->txt_age->setText(qry.value(3).toString());
        }
        login::connClose();
    }
    else
    {
        QMessageBox::critical(this, tr("Error"), qry.lastError().text());
    }
}



the running result:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值