Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. create a child object with initial data
Forum Updated to NodeBB v4.3 + New Features

create a child object with initial data

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 4 Posters 3.8k 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.
  • M Offline
    M Offline
    megido
    wrote on 10 Dec 2019, 11:32 last edited by
    #1

    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?

    J 1 Reply Last reply 10 Dec 2019, 11:44
    0
    • M megido
      10 Dec 2019, 11:32

      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?

      J Offline
      J Offline
      JonB
      wrote on 10 Dec 2019, 11:44 last edited by JonB 12 Oct 2019, 11:47
      #2

      @megido
      Provide a setter in Form for the date value. Have the caller then invoke

      AF->setDate(some_value);
      

      before the AF->show().

      You could alternatively have the Form::Form() constructor accept an explicit date_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.

      M 1 Reply Last reply 10 Dec 2019, 12:08
      3
      • J JonB
        10 Dec 2019, 11:44

        @megido
        Provide a setter in Form for the date value. Have the caller then invoke

        AF->setDate(some_value);
        

        before the AF->show().

        You could alternatively have the Form::Form() constructor accept an explicit date_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.

        M Offline
        M Offline
        megido
        wrote on 10 Dec 2019, 12:08 last edited by megido 12 Oct 2019, 12:23
        #3

        @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

        J 1 Reply Last reply 10 Dec 2019, 12:32
        0
        • M Offline
          M Offline
          megido
          wrote on 10 Dec 2019, 12:23 last edited by megido 12 Oct 2019, 12:23
          #4

          I and JohnB edited our posts. Why posts display an original messages?

          J 1 Reply Last reply 10 Dec 2019, 12:31
          0
          • M megido
            10 Dec 2019, 12:23

            I and JohnB edited our posts. Why posts display an original messages?

            J Offline
            J Offline
            JonB
            wrote on 10 Dec 2019, 12:31 last edited by
            #5

            @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...!

            1 Reply Last reply
            0
            • M megido
              10 Dec 2019, 12:08

              @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

              J Offline
              J Offline
              JonB
              wrote on 10 Dec 2019, 12:32 last edited by
              #6

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

              M 1 Reply Last reply 10 Dec 2019, 12:53
              0
              • J JonB
                10 Dec 2019, 12:32

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

                M Offline
                M Offline
                megido
                wrote on 10 Dec 2019, 12:53 last edited by megido 12 Oct 2019, 12:53
                #7

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

                Then you have your answer!? (Provided you call it from the correct place, as I said.)

                it's called from the correct place.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  megido
                  wrote on 10 Dec 2019, 12:56 last edited by
                  #8

                  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")
                  
                  J 1 Reply Last reply 10 Dec 2019, 13:07
                  0
                  • M megido
                    10 Dec 2019, 12:56

                    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")
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 10 Dec 2019, 13:07 last edited by
                    #9

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

                    M 1 Reply Last reply 10 Dec 2019, 13:09
                    2
                    • J JonB
                      10 Dec 2019, 13:07

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

                      M Offline
                      M Offline
                      megido
                      wrote on 10 Dec 2019, 13:09 last edited by
                      #10

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

                      J 1 Reply Last reply 10 Dec 2019, 13:12
                      0
                      • M megido
                        10 Dec 2019, 13:09

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

                        J Offline
                        J Offline
                        JonB
                        wrote on 10 Dec 2019, 13:12 last edited by
                        #11

                        @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 J.HilkJ 2 Replies Last reply 10 Dec 2019, 13:38
                        3
                        • J JonB
                          10 Dec 2019, 13:12

                          @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 10 Dec 2019, 13:38 last edited by megido 12 Oct 2019, 13:39
                          #12

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

                          J 1 Reply Last reply 10 Dec 2019, 13:44
                          0
                          • M megido
                            10 Dec 2019, 13:38

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

                            J Offline
                            J Offline
                            JonB
                            wrote on 10 Dec 2019, 13:44 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 10 Dec 2019, 13:47
                            0
                            • J JonB
                              10 Dec 2019, 13:44

                              @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 10 Dec 2019, 13:47 last edited by megido 12 Oct 2019, 13:52
                              #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
                              • J JonB
                                10 Dec 2019, 13:12

                                @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 10 Dec 2019, 14:06 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.

                                J 1 Reply Last reply 10 Dec 2019, 14:11
                                2
                                • J.HilkJ J.Hilk
                                  10 Dec 2019, 14:06

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

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 10 Dec 2019, 14:11 last edited by JonB 12 Oct 2019, 14:12
                                  #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 10 Dec 2019, 14:45 last edited by
                                    #17
                                    This post is deleted!
                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      megido
                                      wrote on 10 Dec 2019, 14:46 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

                                      J 1 Reply Last reply 10 Dec 2019, 14:50
                                      1
                                      • M megido
                                        10 Dec 2019, 14:46

                                        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

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 10 Dec 2019, 14:50 last edited by JonB 12 Oct 2019, 14:51
                                        #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 10 Dec 2019, 16:21
                                        0
                                        • J JonB
                                          10 Dec 2019, 14:50

                                          @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 10 Dec 2019, 16:21 last edited by
                                          #20

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

                                          J jsulmJ 2 Replies Last reply 10 Dec 2019, 16:27
                                          0

                                          1/25

                                          10 Dec 2019, 11:32

                                          • Login

                                          • Login or register to search.
                                          1 out of 25
                                          • First post
                                            1/25
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved