码迷,mamicode.com
首页 > 其他好文 > 详细

简易点菜系统

时间:2018-05-19 15:51:19      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:can   win   top   odi   .exe   empty   while   sql   bee   

 1 //main.cpp
 2 #include "mywin.h"
 3 #include "foodtype.h"
 4 #include <QApplication>
 5 #include <QSqlDatabase>
 6 #include <QDebug>
 7 #include <QSqlDriver>
 8 #include <QSqlQuery>
 9 #include <QMessageBox>
10 int main(int argc, char *argv[])
11 {
12     QApplication a(argc, argv);
13    // QSqlDatabase db;
14     QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");//数据库必须在声明的时候指定,否则会出错
15     qDebug()<< db.drivers();//查看支持的数据库
16     //db.addDatabase("QMYSQL");
17     db.setDatabaseName("mydb");
18     db.setHostName("192.168.2.90");
19     db.setUserName("who");
20     db.setPassword("123456");
21     if(!db.open())
22     {
23         QMessageBox::critical(0,"error","open databases failed");
24         exit(0);
25     }
26     qDebug()<<db.tables();
27 
28 
29     QSqlQuery query;
30     query.exec("select *from foodtype;");
31     while(query.next())
32     {
33         qDebug()<<query.value(0).toInt();//转化一下,要不会把数据类型一块打印出来
34         qDebug()<<query.value(1).toString();
35     }
36 
37     qDebug()<<query.exec("show tables;");
38     MyWin w;
39     w.show();
40 
41     FoodType foodtype;
42     foodtype.show();
43     return a.exec();
44 }
 1 //mywin.cpp
 2 #include "mywin.h"
 3 #include "ui_mywin.h"
 4 #include <QSqlQuery>
 5 #include <QDebug>
 6 #include <QMessageBox>
 7 #include <QString>
 8 
 9 MyWin::MyWin(QWidget *parent) :
10     QWidget(parent),
11     ui(new Ui::MyWin)
12 {
13     ui->setupUi(this);
14 
15     QSqlQuery query;
16     query.exec("select *from foodtype;");
17     while(query.next())
18     {
19        // ui->comboBox->addItem(query.value(0).toInt(),query.value(1).toString());//将菜系添加到下拉列表中,必须在后面加上为数据项
20                                                  //,要不后面无法获取fid的下标;
21         ui->comboBox->addItem(query.value(1).toString(),query.value(0).toInt());
22     }
23 }
24 
25 MyWin::~MyWin()
26 {
27     delete ui;
28 }
29 
30 void MyWin::on_pushButton_2_clicked()
31 {
32     this->close();
33 }
34 
35 void MyWin::on_pushButton_clicked()
36 {
37    QSqlQuery query;
38    int fid=ui->comboBox->currentData().toInt();//获取下拉列表里的数据值
39     //qDebug()<<fid;
40    if(ui->line_id->text().isEmpty())
41    {
42        QMessageBox::critical(this,"error","please input id");
43        ui->line_id->setFocus();//聚焦光标
44        return;//exit current function
45    }
46 
47    if(ui->line_name->text().isEmpty())
48    {
49        QMessageBox::critical(this,"error","please input name");
50        ui->line_name->setFocus();//聚焦光标
51        return;
52    }
53    if(ui->line_price->text().isEmpty())
54    {
55        QMessageBox::critical(this,"error","please input price");
56        ui->line_price->setFocus();//聚焦光标
57        return;
58    }
59    //int id=ui->line_id->text().toInt(); //用Qstring 类型声明id,要不下面打印id号时会报错
60    QString id=ui->line_id->text();
61    QString str2("select *from food where fid=%1;");
62    qDebug()<< query.exec(str2.arg(id));
63    while(query.next())
64    {
65        QMessageBox::critical(this,"error","id" + id + "used");
66        ui->line_id->setFocus();
67        return;
68    }
69 
70    QMessageBox mesgBox(this);
71    mesgBox.setText("The document has been modified.");
72    mesgBox.setInformativeText("Do you want to save you change?");
73    mesgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel |QMessageBox::Discard);
74    int ret=mesgBox.exec();
75 
76    QString name=ui->line_name->text();
77    float price=ui->line_price->text().toFloat();
78    QString mem =ui->textEdit->document()->toPlainText();//多行编辑的显示
79 
80    QString str("insert into food values(%1,‘%2‘,%3,%4,‘%5‘);");
81    switch(ret){
82         case QMessageBox::Save:
83                 query.exec(str.arg(id).arg(name).arg(fid).arg(price).arg(mem));
84                 break;
85         case QMessageBox::Cancel:
86                // return;
87                 break;
88        case QMessageBox::Discard:
89                 ui->line_id->setText(" ");
90                 return;
91                 break;
92    }
93 }
 1   //显示tableview里的数据时,要用到《QsqlQueryModel》,
    #include "ui_foodtype.h" 2 //#include <QSqlTableModel> 3 #include <QSqlQueryModel> 4 5 FoodType::FoodType(QWidget *parent) : 6 QWidget(parent), 7 ui(new Ui::FoodType) 8 { 9 ui->setupUi(this); 10 QSqlQueryModel *model=new QSqlQueryModel(this); 11 // model->setTable("菜单信息"); 12 model->setQuery("select fid,food.name,ftypeid,fprice,id,foodtype.name,mem from food inner join foodtype on id=fid;"); 13 14 ui->tableView->setModel(model); 15 } 16 17 FoodType::~FoodType() 18 { 19 delete ui; 20 }

 

简易点菜系统

标签:can   win   top   odi   .exe   empty   while   sql   bee   

原文地址:https://www.cnblogs.com/edan/p/9060311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!