2.1 自定义条目字段:ID、名字、年龄、地址、薪水
2.2 条目类相关代码
***************************************源文件*********************************************** #include "itemform.h" #include "ui_itemform.h" itemForm::itemForm(QWidget *parent) : QMainWindow(parent), ui(new Ui::itemForm) { ui->setupUi(this); } itemForm::~itemForm() { delete ui; } void itemForm::setStaffInfo(int id, QString name, int age, QString addr, int salary) { ui->label_ID->setText(QString::number(id)); ui->label_name->setText(name); ui->label_age->setText(QString::number(age)); ui->label_address->setText(addr); ui->label_salary->setText(QString::number(salary)); } *******************************************头文件******************************************* #ifndef ITEMFORM_H #define ITEMFORM_H #include3、主界面UI设计namespace Ui { class itemForm; } class itemForm : public QMainWindow { Q_OBJECT public: explicit itemForm(QWidget *parent = nullptr); ~itemForm(); void setStaffInfo(int id,QString name,int age,QString addr,int salary); private: Ui::itemForm *ui; }; #endif // ITEMFORM_H
3.1 自定义基本的数据 *** 作:添加、更新、删除、查询
3.2 主界面类相关代码
**************************************源文件********************************************* #include "mainwindow.h" #include "ui_mainwindow.h" #includeMainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); //step1:根据数据库的类型,链接数据 db=QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("compary.db"); //step2:打开数据库 if(!db.open()){ qDebug()<<"open error..."< lineEdit_name->text(); QString salary=ui->lineEdit_salary->text(); QString address=ui->lineEdit_address->text(); QString age=ui->lineEdit_age->text(); QSqlQuery query; //插入数据 *** 作 QString sqlInsert=QString("insert into staff(name,age,address,salary) values('%1',%2,'%3',%4);").arg(name) .arg(age.toInt()) .arg(address) .arg(salary.toInt()); if(!query.exec(sqlInsert)){ qDebug()<<"insert data error..."< lineEdit_ID->text(); QString address=ui->lineEdit_address->text(); //更新数据 *** 作 QString sqlUpdate=QString("update staff set address = '%1' where id = %2;").arg(address) .arg(id.toInt()); if(!query.exec(sqlUpdate)){ qDebug()<<"Update data error..."< lineEdit_ID->text(); //删除 *** 作 QString sqlDelete=QString("delete from staff where id='%1';").arg(id.toInt()); if(!query.exec(sqlDelete)){ qDebug()<<"Delete data error..."< listWidget->clear(); //查询数据 *** 作 QSqlQuery query; QString sqlSelect=QString("select * from staff;"); if(!query.exec(sqlSelect)){ qDebug()<<"Select data error..."< QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_addBtn_clicked(); void on_updateBtn_clicked(); void on_deleteBtn_clicked(); void on_selectBtn_clicked(); private: Ui::MainWindow *ui; QSqlDatabase db; }; #endif // MAINWINDOW_H *************************************主函数*********************************************** #include "mainwindow.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)