Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. create a child object with initial data
Qt 6.12 LTS is out! See what's new in the release blog

create a child object with initial data

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 4 Posters 22.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ JonB

    @megido

    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);
    
    M Offline
    M Offline
    megido
    wrote on last edited by megido
    #12

    @JonB what default argument for QString& is? (in the header)

    JonBJ 1 Reply Last reply
    0
    • M megido

      @JonB what default argument for QString& is? (in the header)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #13

      @megido
      It's up to you whether you choose to have a default value of this argument, and what that is (e.g. ""). You only want a default if you regard that argument as optional. There is no reason why you cannot have it mandatory (no default).

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @megido
        It's up to you whether you choose to have a default value of this argument, and what that is (e.g. ""). You only want a default if you regard that argument as optional. There is no reason why you cannot have it mandatory (no default).

        M Offline
        M Offline
        megido
        wrote on last edited by megido
        #14

        @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&'

        1 Reply Last reply
        0
        • JonBJ JonB

          @megido

          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);
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #15

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

          @megido

          explicit Form(QWidget *parent = 0, const QDate& date_initial_value = QDate::currentDate());


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          JonBJ 1 Reply Last reply
          2
          • J.HilkJ J.Hilk

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

            @megido

            explicit Form(QWidget *parent = 0, const QDate& date_initial_value = QDate::currentDate());

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #16

            @J-Hilk , @megido
            Sorry, I did not see that QWidget *parent had an initialiser = nullptr from what the OP pasted as the definition. Then as @J-Hilk has offered, I would probably go for

            Form::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 precede parent (being optional) anyway, so it's probably best to get into this order habit.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              megido
              wrote on last edited by
              #17
              This post is deleted!
              1 Reply Last reply
              0
              • M Offline
                M Offline
                megido
                wrote on last edited by
                #18

                header:

                explicit Form(const QDate& date_initial_value = QDate::currentDate(), QWidget *parent = 0 );
                

                cpp:

                Form::Form(const QDate& date_initial_value, QWidget *parent):
                

                works properly

                Thanks

                JonBJ 1 Reply Last reply
                1
                • M megido

                  header:

                  explicit Form(const QDate& date_initial_value = QDate::currentDate(), QWidget *parent = 0 );
                  

                  cpp:

                  Form::Form(const QDate& date_initial_value, QWidget *parent):
                  

                  works properly

                  Thanks

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #19

                  @megido
                  I am glad you have a solution. However, would you like to explain why you need that value to go ui->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?

                  M 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @megido
                    I am glad you have a solution. However, would you like to explain why you need that value to go ui->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?

                    M Offline
                    M Offline
                    megido
                    wrote on last edited by
                    #20

                    @JonB
                    I just don't know another way if it exists

                    JonBJ jsulmJ 2 Replies Last reply
                    0
                    • M megido

                      @JonB
                      I just don't know another way if it exists

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #21

                      @megido
                      I can't go through it again. I said that your original way was fine, with no parameters

                        Form *AF =  new Form;
                        // here a few connects()
                        AF->set_user_date(user_date);
                        AF->show();
                      

                      I told you that was fine. You then started saying the date was not initialised in the constructor. I replied:

                      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.

                      You didn't say anything about why you have to have it initialized in the constructor. I do not see why you care about the log messages, they are no problem. You just started asking how to pass it as a parameter. So that is what I answered.

                      The best way is your original code. Answer why you care about when it is initilalized, what is unacceptable about this code?

                      M 1 Reply Last reply
                      0
                      • M megido

                        @JonB
                        I just don't know another way if it exists

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #22

                        @megido Can you show how you implemented set_user_date()?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        M 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @megido Can you show how you implemented set_user_date()?

                          M Offline
                          M Offline
                          megido
                          wrote on last edited by
                          #23

                          @jsulm I have delete that. it was a private void

                          jsulmJ 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @megido
                            I can't go through it again. I said that your original way was fine, with no parameters

                              Form *AF =  new Form;
                              // here a few connects()
                              AF->set_user_date(user_date);
                              AF->show();
                            

                            I told you that was fine. You then started saying the date was not initialised in the constructor. I replied:

                            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.

                            You didn't say anything about why you have to have it initialized in the constructor. I do not see why you care about the log messages, they are no problem. You just started asking how to pass it as a parameter. So that is what I answered.

                            The best way is your original code. Answer why you care about when it is initilalized, what is unacceptable about this code?

                            M Offline
                            M Offline
                            megido
                            wrote on last edited by megido
                            #24

                            @JonB yes, I have understood you. I too long time ago developed on Qt and forgot many. In this situation a just should call in the setter.

                            ui->dateedit->setDate(date);
                            

                            but instead that I tried to update the variable before using it in the constructor. Because I thought what I really should predefine values in fields before the draw(It's usual for the WEB development).

                            1 Reply Last reply
                            0
                            • M megido

                              @jsulm I have delete that. it was a private void

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #25

                              @megido said in create a child object with initial data:

                              I have delete that. it was a private void

                              Why not making it public and use instead of deleting?

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt
                              • Unsolved