Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QDialog: How to change value from subclassed QSortFilterProxyModel in QLineEdit and submit to database?



  • Hi,

    I have some questions about the QDialog. First, I would like to briefly explain my scenario, what I have already done and what I want to achieve.

    I have a QListView in which I select a record, then click on a QPushButton to open my QDialog to edit the record. I also know that I can edit directly in a QListView, but for some reasons I don't want to. My QDialog code looks like this:

    SynonymEditingDialog.cpp

    #include "synonymeditingdialog.h"
    #include "ui_synonymeditingdialog.h"
    
    SynonymEditingDialog::SynonymEditingDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::synonymEditingDialog)
    {
        ui->setupUi(this);
    
        mMapper = new QDataWidgetMapper;
    
    }
    
    SynonymEditingDialog::~SynonymEditingDialog()
    {
        delete ui;
    }
    
    void SynonymEditingDialog::setEditingModel(QAbstractItemModel *model) const
    {
        //model = subclassed QSortFilerProxModel called mSynonymFilterModel, sourceModel is a QSqlTableModel
        mMapper->setModel(model);
        mMapper->addMapping(ui->synonym, 1);
    }
    
    void SynonymEditingDialog::setEditingIndex(const QModelIndex &index) const
    {
        //set the Index from Model to QLineEdit
        mMapper->setCurrentModelIndex(index);
    }
    
    void SynonymEditingDialog::on_cancelButton_clicked()
    {
        
        //Check if Model has changed and do a warning befor Closing (Warning will de done via QMessageBox)
        this->close();
    }
    
    
    void SynonymEditingDialog::on_saveButton_clicked()
    {
        //Want to close Window and Update subclassed QSortFilerProxyModel, but do not save to database (only Update Model), saving to database will be done in MainWindow on save Button
    }
    
    
    void SynonymEditingDialog::on_removeButton_clicked()
    {
        //remove entry only from Model, permantetly remove it, if I click in MainWindow on save Button
    }
    

    SynonymEditingDialog.h

    #ifndef SYNONYMEDITINGDIALOG_H
    #define SYNONYMEDITINGDIALOG_H
    
    #include <QDialog>
    #include <QModelIndex>
    #include <QDataWidgetMapper>
    #include <QSortFilterProxyModel>
    
    namespace Ui {
    class synonymEditingDialog;
    }
    
    class SynonymEditingDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit SynonymEditingDialog(QWidget *parent = nullptr);
        ~SynonymEditingDialog();
        void setEditingModel(QAbstractItemModel *model) const;
        void setEditingIndex(const QModelIndex &index) const;
    
    private slots:
        void on_cancelButton_clicked();
        void on_saveButton_clicked();
        void on_removeButton_clicked();
    
    private:
        Ui::synonymEditingDialog *ui;
        QDataWidgetMapper *mMapper;
    };
    
    #endif // SYNONYMEDITINGDIALOG_H
    
    

    And my mainwindow.cpp do that:

    void MainWindow::on_synonymButton_clicked()
    {
        SynonymEditingDialog *synonymEditing = new SynonymEditingDialog(this);
    
        if(mToggleListView->selectionModel()->hasSelection()){
            QModelIndex index = mToggleListView->selectionModel()->selectedIndexes().first();
            synonymEditing->setEditingModel(mSynonymFilterModel);  //mSynonymFilterModel is a subclassed QSortFilerProxyModel, sourceModel is a QSqlTableModel
            synonymEditing->setEditingIndex(index);
            synonymEditing->exec();
        }
        if(synonymEditing->close())
            delete  synonymEditing;
    }
    

    And if I click on my save Button in MainWindow I do that:

    void MainWindow::on_saveButton_clicked()
    {
        mMapper->submit(); //mMapper of MainWindow not SynonymEditingDialog!
        mSynonymFilterModel->sourceModel()->submit(); //nothing happend and submitAll is not avaiable
    }
    

    I already have it so far that the corresponding record is displayed in my QLineEdit of the QDialog and I can change it. What I am still missing are the comments in the text. Can someone perhaps give me a tip?

    For improvement suggestions of my code I am grateful and open for.

    Thanks a lot


  • Lifetime Qt Champion

    Hi,

    @Gabber said in QDialog: How to change value from subclassed QSortFilterProxyModel in QLineEdit and submit to database?:

    MainWindow::on_saveButton_clicked

    Why did you implement it in MainWindow rather than in your dialog ?



  • @SGaist said in QDialog: How to change value from subclassed QSortFilterProxyModel in QLineEdit and submit to database?:

    Why did you implement it in MainWindow rather than in your dialog ?

    Because the MainWindow still contains a few QLineEdits and a QPlainTextEdit and all data should be stored together so that the user does not get confused.
    The QDIalog should only be used to edit the selected element and to update the model. In the MainWindow there is a Save button that should write all data into the database.


  • Lifetime Qt Champion

    My bad the code scrolling made me miss parts of the dialog content.

    So from the looks it you rather want to have a "cache editor" and act when you come back from the dialog. Thus you should rather load the dialog with the data and then decide what to do based on the returned value of exec.



  • @SGaist

    I will try that with the return value. Do you have any ideas / tips how I can implement the comments in SynonymEditingDialog.cpp? I still lack the necessary knowledge. A tip / documentation or even code where I can read and learn things would be ideal!

    Thanks for your support!


  • Lifetime Qt Champion

    Use a dirty flag when one of the value has changed. Don't touch the model at all.


Log in to reply