zoukankan      html  css  js  c++  java
  • 简易点菜系统

     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 }
  • 相关阅读:
    程序人生,编程思想
    CentOS Linux Jenkins安装、部署、更新
    Git常用命令
    U盘安装Mac OS X要点
    Shell执行*.sql
    WebStorm远程调试Node.js
    svn常用命令
    敏捷开发相关编辑思想(SOA、DDD、REST、CQRS)
    VisualVM远程监控Java
    centos搭建git服务
  • 原文地址:https://www.cnblogs.com/edan/p/9060311.html
Copyright © 2011-2022 走看看