diff --git a/admin/mainwindow.cpp b/admin/mainwindow.cpp index cd55cdb47d6f01fee416af0171ac610b5e674f15..872691333f4785fc91da6ccce90692f7e0f5201b 100644 --- a/admin/mainwindow.cpp +++ b/admin/mainwindow.cpp @@ -257,8 +257,8 @@ MainWindow::MainWindow(QWidget *parent) : inventoryModel->setEditStrategy(QSqlTableModel::OnFieldChange); - // QObject::connect(inventoryModel, &QAbstractItemModel::dataChanged, - // this, &MainWindow::on_inventoryModel_dataChanged); + QObject::connect(inventoryModel, &QAbstractItemModel::dataChanged, + this, &MainWindow::on_inventoryModel_dataChanged); // inventoryModel->setRelation(1, QSqlRelation("products", "id", "name")); inventoryModel->select(); @@ -272,9 +272,6 @@ MainWindow::MainWindow(QWidget *parent) : ui->inventoryCheckView->hideColumn(1); ui->inventoryCheckView->hideColumn(2); - // TODO this line makes stuff dis-a-pear - inventoryModel->setData( inventoryModel->index(0, 5) , QVariant(5)); - // -------------------------------------------------- setMoneyDiffLabels(); @@ -331,20 +328,30 @@ MainWindow::~MainWindow() delete ui; } +/* + * This updates the QSqlTableModel to contain the same data as the database. + * This is needed since it looks at a view, and can therefore not find the + * primary key. Requiring all the fields in the front-end and back-end database + * to have the exact same fields. + * + * So, the calculations here are also done in the database, and those are what + * should be used when doing further stuff with the data. + */ void MainWindow::on_inventoryModel_dataChanged(const QModelIndex &topLeft, const QModelIndex& /*bottomRight*/, const QVector& /*roles*/) { - /* - if (topLeft.column() != 4) return; + QSqlTableModel* model = (QSqlTableModel*) ui->inventoryCheckView->model(); - DisablingModel* model = (DisablingModel*) ui->inventoryCheckView->model(); - int row = topLeft.row(); - QModelIndex index = model->index(row, 3); - int expected = model->data(index).toInt(); - int actual = model->data(topLeft).toInt(); - int diff = actual - expected - model->setData(model->index(row, 5), diff); - */ - qDebug() << "Data Chaned" << topLeft; + if (model->isDirty()) { + int row = topLeft.row(); + + bool number; + int expected = model->data(model->index(row, 4)).toInt(); + int actual = model->data(model->index(row, 5)).toInt(&number); + + model->setData(model->index(row, 6), + number ? expected - actual + : QVariant()); + } } /*