How to pass QSqlRelationalTableModel object to a class constructor (from main.cpp)
-
For example, partwidget.h:
explicit PartWidget(QWidget parent = nullptr, QSqlRelationalTableModel model = new QSqlRelationalTableModel());partwidget.cpp:
PartWidget::PartWidget(QWidget parent, QSqlRelationalTableModel model)
: QWidget(parent)
, ui(new Ui::PartWidget)
, model(model)
{
ui->setupUi(this);
} -
If you really need to pass the model around (maybe there's a better design to achieve what you are trying to do) then the same way like you would pass any other
QObject
-based class:explicit PartWidget(QSqlRelationalTableModel *model = nullptr, QWidget *parent = nullptr);
PartWidget::PartWidget(QSqlRelationalTableModel *model, QWidget *parent) : QWidget(parent) , ui(new Ui::PartWidget) , m_model(model) { ui->setupUi(this); }
This is more a C++ OOP question
And please use Code Tags the next time you post code snippets.Edit:
As I just read the title again:
Why create the model in main and then pass it to some widget in the first place?
Why not create the model right in the widget where you need it?!
There shouldn't be too much going on in yourmain.cpp
. -
@Pl45m4 Code tags is like code in your answer displayed ? I just trying do not create unnecessary objects of QSqlRelationalTableModel, so i thought better just pass it through widgets. Can you recommend C++ or OOP related book ? Thank you for answer !
-
@Gandalf404 said in How to pass QSqlRelationalTableModel object to a class constructor (from main.cpp):
I just trying do not create unnecessary objects of QSqlRelationalTableModel, so i thought better just pass it through widgets
Like I've said, create it in your widget where you need it.
That would make one instance only.
Or for what reason you need it inmain()
?