From ea75e57dbfc8ef1b78c73f51e18bae3d4208b1cf Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Mon, 8 Dec 2025 13:26:42 +0100 Subject: [PATCH] An item can be added to the model with accepting the NewItemDialog. --- Dialogs/newitemdialog.cpp | 20 ++++++++++++++++++++ Dialogs/newitemdialog.h | 10 ++++++++++ mainwindow.cpp | 11 ++++++----- mainwindow.h | 4 ++-- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Dialogs/newitemdialog.cpp b/Dialogs/newitemdialog.cpp index f0159dd..e93989c 100644 --- a/Dialogs/newitemdialog.cpp +++ b/Dialogs/newitemdialog.cpp @@ -1,6 +1,8 @@ #include "newitemdialog.h" #include +#include +#include #include #include #include @@ -58,3 +60,21 @@ void NewItemDialog::createContent() { m_outerLayout->insertWidget(0, m_contentContainer); } + +void NewItemDialog::accept() { + QJsonObject itemObject; + itemObject.insert("Name", m_nameEdit->text()); + itemObject.insert("Description", m_descriptionEdit->text()); + itemObject.insert("Info", m_infoEdit->text()); + itemObject.insert("Amount", m_amountBox->value()); + itemObject.insert("Factor", m_factorBox->value()); + + QJsonDocument jsonDoc; + QJsonArray itemArray; + itemArray.append(itemObject); + jsonDoc.setArray(itemArray); + emit addItems(jsonDoc.toJson(QJsonDocument::Compact)); + + // resetContent(); + AbstractDialog::accept(); +} diff --git a/Dialogs/newitemdialog.h b/Dialogs/newitemdialog.h index c1f3655..678f8dd 100644 --- a/Dialogs/newitemdialog.h +++ b/Dialogs/newitemdialog.h @@ -3,15 +3,25 @@ #include "abstractdialog.h" +class QDoubleSpinBox; class QLineEdit; class QSpinBox; class QLabel; class NewItemDialog : public AbstractDialog { + Q_OBJECT + public: NewItemDialog(QWidget* parent = nullptr); void createContent() override; + signals: + void addItems(const QByteArray& jsonDoc); + + public slots: + void accept() override; + // void reject() override; + private: QLabel* m_nameLabel = nullptr; QLineEdit* m_nameEdit = nullptr; diff --git a/mainwindow.cpp b/mainwindow.cpp index 63215c4..7b63b11 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -8,6 +8,7 @@ #include "Dialogs/newitemdialog.h" #include "data/settingshandler.h" #include "genericcore.h" +#include "model/tablemodel.h" static QString updateTextClean = "Do you want to update the application now?"; static QString updateTextDirty = "Do you want to save the tasks & update the application now?"; @@ -35,6 +36,9 @@ MainWindow::MainWindow(QWidget* parent) restoreGeometry(settings.value("geometry").toByteArray()); restoreState(settings.value("windowState").toByteArray()); + m_tableModel = m_core->getModel(); + ui->tableView->setModel(m_tableModel.get()); + createActions(); createHelpMenu(); createGuiDialogs(); @@ -44,9 +48,6 @@ MainWindow::MainWindow(QWidget* parent) connect(this, &MainWindow::displayStatusMessage, this, &MainWindow::showStatusMessage); connect(this, &MainWindow::checkForUpdates, this, &MainWindow::on_actionCheck_for_update_triggered, Qt::QueuedConnection); - - m_tableModel = m_core->getModel(); - ui->tableView->setModel(m_tableModel.get()); } MainWindow::~MainWindow() { delete ui; } @@ -129,8 +130,6 @@ void MainWindow::on_pushButton_clicked() { void MainWindow::openNewItemDialog() { showStatusMessage(tr("Invoked 'Edit|New Item'")); - qInfo() << "'open new item dialog' action triggered..."; - m_newItemDialog->show(); } @@ -268,4 +267,6 @@ void MainWindow::createHelpMenu() { void MainWindow::createGuiDialogs() { m_newItemDialog = make_unique(this); m_newItemDialog->createContent(); + connect(m_newItemDialog.get(), &NewItemDialog::addItems, m_tableModel.get(), + &TableModel::appendItems); } diff --git a/mainwindow.h b/mainwindow.h index c57565c..ae1cebb 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,7 +4,7 @@ #include class NewItemDialog; -class QAbstractItemModel; +class TableModel; QT_BEGIN_NAMESPACE @@ -46,7 +46,7 @@ class MainWindow : public QMainWindow { // GenericCore* m_core; unique_ptr m_core; - shared_ptr m_tableModel; + shared_ptr m_tableModel; /// File actions unique_ptr m_newFileAct;