How can a child form use parent 's form model
-
I have a form named 'ArticlesWindow' and I open another form named 'addArticle' this way:
void ArticlesWindow::on_newArticle_btn_clicked() { class addArticle *m = new class addArticle (); m->setModal(true); m->setAttribute(Qt::WA_DeleteOnClose); m->show(); }
With form 'addArticle' I want to add a new Article, insert a new one.
So I will have to use the model (a QSqlRelationalTableModel) of the parent form ('ArticlesWindow') from within the child form.How can I do this?
-
I have a form named 'ArticlesWindow' and I open another form named 'addArticle' this way:
void ArticlesWindow::on_newArticle_btn_clicked() { class addArticle *m = new class addArticle (); m->setModal(true); m->setAttribute(Qt::WA_DeleteOnClose); m->show(); }
With form 'addArticle' I want to add a new Article, insert a new one.
So I will have to use the model (a QSqlRelationalTableModel) of the parent form ('ArticlesWindow') from within the child form.How can I do this?
@Panoss
Pass a pointer or a reference to theQSqlRelationalTableModel
from the parent form into the child form. You could do that by adding a parameter to child's constructor, or have an explicitsetModel()
method exposed after construction.Alternatively, it isn't always a good idea to pass models around and have UI windows/dialogs update it. It may be better to have your sub-forms gather information from the user, put it into a
struct
/class
or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates. Can help to keep things "tidy" like that, then the UI forms don't have their own logic for updating the data, when over time you find you wish to re-use code or manage it all in one place. -
@Panoss
Pass a pointer or a reference to theQSqlRelationalTableModel
from the parent form into the child form. You could do that by adding a parameter to child's constructor, or have an explicitsetModel()
method exposed after construction.Alternatively, it isn't always a good idea to pass models around and have UI windows/dialogs update it. It may be better to have your sub-forms gather information from the user, put it into a
struct
/class
or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates. Can help to keep things "tidy" like that, then the UI forms don't have their own logic for updating the data, when over time you find you wish to re-use code or manage it all in one place.@JonB said in How can a child form use parent 's form model:
@Panoss
It may be better to have your sub-forms gather information from the user, put it into astruct
/class
or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates.But if something goes wrong with the new record's insertion, then the parent form will have to emit a signal back to the child...
I 'll try this but sounds quite complicated. -
@JonB said in How can a child form use parent 's form model:
@Panoss
It may be better to have your sub-forms gather information from the user, put it into astruct
/class
or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates.But if something goes wrong with the new record's insertion, then the parent form will have to emit a signal back to the child...
I 'll try this but sounds quite complicated. -
@Panoss
Pass a pointer or a reference to theQSqlRelationalTableModel
from the parent form into the child form. You could do that by adding a parameter to child's constructor, or have an explicitsetModel()
method exposed after construction.Alternatively, it isn't always a good idea to pass models around and have UI windows/dialogs update it. It may be better to have your sub-forms gather information from the user, put it into a
struct
/class
or whatever and have it emit a signal with the information. Then one single place, like the main form, can have a slot for that to do the actual model/database updates. Can help to keep things "tidy" like that, then the UI forms don't have their own logic for updating the data, when over time you find you wish to re-use code or manage it all in one place.@JonB said in How can a child form use parent 's form model:
@Panoss
Pass a pointer or a reference to theQSqlRelationalTableModel
from the parent form into the child form. You could do that by adding a parameter to child's constructorI tried this but obviously I 'm not doing ig correctly:
#define ADDARTICLE_H #include <QDialog> #include <QtSql> namespace Ui { class addArticle; } class addArticle : public QDialog { Q_OBJECT public: explicit addArticle(QWidget *parent = nullptr, QSqlRelationalTableModel *model); ~addArticle(); private: Ui::addArticle *ui; }; #endif // ADDARTICLE_H
(the code I added is: ', QSqlRelationalTableModel *model' and I get error "addarticle.h:16:78: error: missing default argument on parameter 'model'")
-
@JonB said in How can a child form use parent 's form model:
@Panoss
Pass a pointer or a reference to theQSqlRelationalTableModel
from the parent form into the child form. You could do that by adding a parameter to child's constructorI tried this but obviously I 'm not doing ig correctly:
#define ADDARTICLE_H #include <QDialog> #include <QtSql> namespace Ui { class addArticle; } class addArticle : public QDialog { Q_OBJECT public: explicit addArticle(QWidget *parent = nullptr, QSqlRelationalTableModel *model); ~addArticle(); private: Ui::addArticle *ui; }; #endif // ADDARTICLE_H
(the code I added is: ', QSqlRelationalTableModel *model' and I get error "addarticle.h:16:78: error: missing default argument on parameter 'model'")
@Panoss said in How can a child form use parent 's form model:
explicit addArticle(QWidget *parent = nullptr, QSqlRelationalTableModel *model);
C++: You cannot have a parameter with a default (an "optional" parameter) before a compulsory parameter! For
QWidget
classes, keepparent
parameter as the last one:addArticle(QSqlRelationalTableModel *model, QWidget *parent = nullptr);
-
This is how i did , but it crashes on the last line:
addArticle::addArticle(QSqlRelationalTableModel *model, QWidget *parent) : QDialog(parent), ui(new Ui::addArticle){ ui->setupUi(this); //this->model=model; // Table: articles, Fields: id, name, position // Remember the indexes of the columns: positionIdx = model->fieldIndex("position"); // bound field for combo is field 'position' ui->positionEdit->setModel(model->relationModel(positionIdx)); // display field for combo is field 'name' ui->positionEdit->setModelColumn(model->relationModel(positionIdx)->fieldIndex("name")); }
(ui->positionEdit is a combo box on the child form)
-
This is how i did , but it crashes on the last line:
addArticle::addArticle(QSqlRelationalTableModel *model, QWidget *parent) : QDialog(parent), ui(new Ui::addArticle){ ui->setupUi(this); //this->model=model; // Table: articles, Fields: id, name, position // Remember the indexes of the columns: positionIdx = model->fieldIndex("position"); // bound field for combo is field 'position' ui->positionEdit->setModel(model->relationModel(positionIdx)); // display field for combo is field 'name' ui->positionEdit->setModelColumn(model->relationModel(positionIdx)->fieldIndex("name")); }
(ui->positionEdit is a combo box on the child form)
@Panoss said in How can a child form use parent 's form model:
but it crashes on the last line
That's what a debugger is for! Run it in debugger, allow to crash, look at stack trace window.
If a line "crashes", break it into separate code to test intermediate results. For all I know there may not even be a
fieldIndex("name")
....