create a child object with initial data
-
for example, I have an widget
Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) { // Form ui have inputs ui->dateedit->setDate(initial_value); }
Then we show that when we need it.
Form *AF = new Form; AF->show();
But I need to set up an initial data for input. I tried to add and call a setter but it's called after widget drawing.
How to handle it?
-
@megido
Provide a setter inForm
for the date value. Have the caller then invokeAF->setDate(some_value);
before the
AF->show()
.You could alternatively have the
Form::Form()
constructor accept an explicitdate_initial_value
argument, but that does not scale well if you have further arguments for further widgets, unless there is something specially fundamental about the initial date only which means you want to pass it to the constructor. -
@JonB said in create a child object with initial data:
Provide a setter in Form for the date value. Have the caller then invoke
I have made it already.
UPD:
log messages(message in Widget init) user_date: QDate("") (message in setter) user_date: QDate("2019-12-24")
setter executing after init
-
@megido said in create a child object with initial data:
I and JohnB edited our posts. Why posts display an original messages?
Sadly because of on-going https://forum.qt.io/topic/109113/server-adjustments-10-00-cet-friday-22nd-november/.
To resolve: sit and click your browser's "refresh" on & on, until eventually you will find it picks up the edited posts...! -
@megido said in create a child object with initial data:
@JonB said in create a child object with initial data:
Provide a setter in Form for the date value. Have the caller then invoke
I have made it already.
Then you have your answer!? (Provided you call it from the correct place, as I said.)
-
Why my messages edits dissapeared. What a hell???
My current code is:
Form *AF = new Form; // here a few connects() AF->set_user_date(user_date); AF->show();
This is log messages in original order
(message in Widget init) user_date: QDate("") (message in setter) user_date: QDate("2019-12-10")
-
@megido
I don't see what there is to say. Yes, the date is empty/default during the constructor, it gets changed to the desired date when the setter is called. What else would you expect, and what is the problem? Like I wrote, if you really need it to be set during the constructor you would have to add a parameter for it, but there is no obvious reason why you would need that. -
@JonB said in create a child object with initial data:
@megido
I don't see what there is to say. Yes, the date is empty/default during the constructor, it gets changed to the desired date when the setter is called. What else would you expect, and what is the problem? Like I wrote, if you really need it to be set during the constructor you would have to add a parameter for it, but there is no obvious reason why you would need that.ok, how I can load forms after the parameter is setted?
-
Form::Form(QWidget *parent, const QString& date_initial_value) : QWidget(parent), ui(new Ui::Form) { // Form ui have inputs ui->dateedit->setDate(date_initial_value); } QString date_initial_value = "some value"; Form *AF = new Form(date_initial_value);
-
-
@JonB I asking because I have set a
public: explicit Form(QWidget *parent = 0, QDate& date_initial_value = QDate::currentDate()); ~Form();
and I have tried QString& date_initial_value = QString("")
but I get an error could not convert 'QDate()' from 'QDate' to 'QDate&'
-
@JonB said in create a child object with initial data:
Form::Form(QWidget *parent, const QString& date_initial_value) :
QWidget(parent),
ui(new Ui::Form)
{just one point, as QWidget *parent usually has a nullptr as default, therefore
Form::Form(QWidget *parent, const QString& date_initial_value)
requires for date_initial_value to also have a default argument
usually one places the arguments that have to be passed first
Form::Form(const QString& date_initial_value, QWidget *parent)
explicit Form(QWidget *parent = 0, const QDate& date_initial_value = QDate::currentDate());
-
@J-Hilk , @megido
Sorry, I did not see thatQWidget *parent
had an initialiser= nullptr
from what the OP pasted as the definition. Then as @J-Hilk has offered, I would probably go forForm::Form(const QString& date_initial_value = QDate::currentDate(), QWidget *parent = nullptr)
because of the convention (I believe) elsewhere that
parent
always comes last? If you do have any mandatory parameters they are going to have to precedeparent
(being optional) anyway, so it's probably best to get into this order habit. -
@megido
I am glad you have a solution. However, would you like to explain why you need that value to goui->dateedit->setDate()
in the constructor anyway? You have had to write extra, complicated constructor code to implement, and as I said it won't scale nicely if you add further parameters. I can't see why you would need to set its value as soon as the constructor, calling the setter explicitly after construction but before showing is the usual way to do things, so what's different about your case?