Advertisement
Guest User

Practice

a guest
May 14th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. HPP FILE
  2.  
  3. #ifndef DIALOG_HPP
  4. #define DIALOG_HPP
  5.  
  6. #include "ui_dialog.h"
  7. #include <QApplication>
  8. #include <QDialog>
  9. #include <QSqlTableModel>
  10. #include <QTableView>
  11. #include <QSqlQuery>
  12. #include <QFileDialog>
  13. #include <QMessageBox>
  14.  
  15. namespace Ui {
  16. class Dialog;
  17. }
  18.  
  19. class Dialog : public QDialog, private Ui::Dialog
  20. {
  21.     Q_OBJECT
  22.  
  23. public:
  24.     explicit Dialog(QWidget *parent = 0);
  25.     ~Dialog();
  26.  
  27.     void add(const QString &item, int price, int quantity, int day, int month, int year);
  28.     //void stat();
  29.  
  30. protected:
  31.     void changeEvent(QEvent *e);
  32.  
  33. private slots:
  34.     void on_addButton_clicked();
  35.  
  36. private:
  37.     Ui::Dialog *ui;
  38.     QSqlTableModel *model;
  39.     QSqlDatabase db;
  40.     QSqlQuery query;
  41.     QString file;
  42. };
  43.  
  44. #endif // DIALOG_HPP
  45.  
  46. CPP FILE
  47.  
  48. #include "dialog.hpp"
  49. #include "QLatin1String"
  50. #include <QDebug>
  51.  
  52. void Dialog::add(const QString &item, int price, int quantity, int day, int month, int year)
  53. {
  54.     query.prepare(QLatin1String("insert into db (item, price, quantity, day, month, year) values (?, ?, ?, ?, ?, ?"));
  55.     query.addBindValue(item);
  56.     query.addBindValue(price);
  57.     query.addBindValue(quantity);
  58.     query.addBindValue(day);
  59.     query.addBindValue(month);
  60.     query.addBindValue(year);
  61.     query.exec();
  62.     query.clear();
  63. }
  64. Dialog::Dialog(QWidget *parent) :
  65.     QDialog(parent),ui(new Ui::Dialog), db(QSqlDatabase::addDatabase("QSQLITE"))
  66. {
  67.     //SQL FILE
  68.     file = QFileDialog::getOpenFileName(this, tr("Open Database"), "/", tr("SQLite Database Files (*.sqlite)"));
  69.     if (file.isNull())
  70.     {
  71.         qDebug() << " in";
  72.         QMessageBox::critical(this, "Error", "You must choose some file!");
  73.         parent->close();
  74.     }
  75.     qDebug() << "out";
  76.     // БД
  77.     db.setDatabaseName(file);
  78.     db.open();
  79.     if (db.tables().contains(QLatin1String("db")) == false)
  80.     {
  81.         // Создаем таблицу бд
  82.         query.exec(QLatin1String("create table db (id integer primary key, item varchar, price integer, quantity integer, day integer, month integer, year integer)"));
  83.         query.clear();
  84.         qDebug() << "in2";
  85.         // Добавим что-нибудь
  86.         add("iPhone 5s 64GB", 399, 12, 22, 01, 1998);
  87.         add("iPad 2 32GB", 499, 12, 22, 04 ,1991);
  88.     }
  89.     qDebug() << "out 2";
  90.     //Интерфейс
  91.     setupUi(this);
  92.  
  93.     // Создаем таблицу бд
  94.     query.exec(QLatin1String("create table db (id integer primary key, item varchar, price integer, quantity integer, day integer, month integer, year integer)"));
  95.     query.clear();
  96.     qDebug() << "out 3";
  97.     //Создадим модель
  98.     model = new QSqlTableModel(this,db);
  99.     qDebug() << "out 4";
  100.     model->setTable("db");
  101.     model->setEditStrategy(QSqlTableModel::OnFieldChange);
  102.     model->setHeaderData(0, Qt::Horizontal, tr("ID"));
  103.     model->setHeaderData(1, Qt::Horizontal, tr("Item"));
  104.     model->setHeaderData(2, Qt::Horizontal, tr("Price"));
  105.     model->setHeaderData(3, Qt::Horizontal, tr("Quantity"));
  106.     model->setHeaderData(4, Qt::Horizontal, tr("Day"));
  107.     model->setHeaderData(5, Qt::Horizontal, tr("Month"));
  108.     model->setHeaderData(6, Qt::Horizontal, tr("Year"));
  109.     model->select();
  110.     qDebug() << "out 5";
  111.     // Добавим модель
  112.     ui->tableView->setModel(model);
  113.     ui->tableView->show();
  114.     qDebug() << "out 6";
  115.  
  116. }
  117.  
  118. void Dialog::changeEvent(QEvent *e)
  119. {
  120.     QDialog::changeEvent(e);
  121.     switch (e->type()) {
  122.     case QEvent::LanguageChange:
  123.         retranslateUi(this);
  124.         break;
  125.     default:
  126.         break;
  127.     }
  128. }
  129.  
  130. Dialog::~Dialog()
  131. {
  132.     delete ui;
  133.     delete model;
  134. }
  135.  
  136. void Dialog::on_addButton_clicked()
  137. {
  138.     add(ui->nameEdit->text(),ui->priceBox->value(),ui->quantityBox->value(),ui->dayBox->value(),ui->monthBox->value(),ui->yearBox->value());
  139.     ui->nameEdit->clear();
  140.     ui->priceBox->clear();
  141.     ui->quantityBox->clear();
  142.     ui->dayBox->clear();
  143.     ui->monthBox->clear();
  144.     ui->yearBox->clear();
  145.     model->select();
  146. }
  147.  
  148.  
  149. MAIN FILE
  150.  
  151. #include "dialog.hpp"
  152. #include <QApplication>
  153.  
  154. int main(int argc, char *argv[])
  155. {
  156.     QApplication a(argc, argv);
  157.     Dialog w;
  158.     w.show();
  159.  
  160.     return a.exec();
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement