Important: Please read the 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:


    #include "synonymeditingdialog.h"
    #include "ui_synonymeditingdialog.h"
    SynonymEditingDialog::SynonymEditingDialog(QWidget *parent) :
        ui(new Ui::synonymEditingDialog)
        mMapper = new QDataWidgetMapper;
        delete ui;
    void SynonymEditingDialog::setEditingModel(QAbstractItemModel *model) const
        //model = subclassed QSortFilerProxModel called mSynonymFilterModel, sourceModel is a QSqlTableModel
        mMapper->addMapping(ui->synonym, 1);
    void SynonymEditingDialog::setEditingIndex(const QModelIndex &index) const
        //set the Index from Model to QLineEdit
    void SynonymEditingDialog::on_cancelButton_clicked()
        //Check if Model has changed and do a warning befor Closing (Warning will de done via QMessageBox)
    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


    #include <QDialog>
    #include <QModelIndex>
    #include <QDataWidgetMapper>
    #include <QSortFilterProxyModel>
    namespace Ui {
    class synonymEditingDialog;
    class SynonymEditingDialog : public QDialog
        explicit SynonymEditingDialog(QWidget *parent = nullptr);
        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();
        Ui::synonymEditingDialog *ui;
        QDataWidgetMapper *mMapper;

    And my mainwindow.cpp do that:

    void MainWindow::on_synonymButton_clicked()
        SynonymEditingDialog *synonymEditing = new SynonymEditingDialog(this);
            QModelIndex index = mToggleListView->selectionModel()->selectedIndexes().first();
            synonymEditing->setEditingModel(mSynonymFilterModel);  //mSynonymFilterModel is a subclassed QSortFilerProxyModel, sourceModel is a QSqlTableModel
            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


    @Gabber 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 ?

  • @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