Results 1 to 8 of 8

Thread: what does the constructor do here?

  1. #1
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    4

    Default what does the constructor do here?

    is this a type of initialization? the constructor ButtonWidget(int widgets) seems doesnt initialize int widget. could anyone explain it in detail=? thank you.
    I donot understand the code in buttonwidget.cpp
    Qt Code:
    1. //buttonwidget.cpp
    2. ButtonWidget::ButtonWidget( int widgets )
    3. : QWidget()
    4. {
    5. QVBoxLayout *vbox = new QVBoxLayout();
    6.  
    7. if ( widgets & ButtonWidget::ComboBoxes )
    8. vbox->addWidget( comboBoxes() );
    9.  
    10. if ( widgets & ButtonWidget::SpinBoxes )
    11. vbox->addWidget( spinBoxes( widgets & ButtonWidget::SpinBoxexUpdateButton ) );
    12.  
    13. if ( widgets & ButtonWidget::CheckBoxes )
    14. vbox->addWidget( checkBoxes() );
    15.  
    16. if ( widgets & ButtonWidget::ParetoBoxes )
    17. vbox->addWidget( paretoBoxes() );
    18.  
    19. if ( widgets & ButtonWidget::MarknDiffBoxes )
    20. vbox->addWidget( markndiffBoxes() );
    21.  
    22. vbox->addStretch();
    23.  
    24. setLayout(vbox);
    25. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //buttonwidget.h
    2. ........
    3. class ButtonWidget : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. enum Widgets
    9. {
    10. ComboBoxes = 1,
    11. SpinBoxes = 2,
    12. SpinBoxexUpdateButton = 4,
    13. CheckBoxes = 8,
    14. ParetoBoxes = 16,
    15. MarknDiffBoxes = 32
    16. };
    17.  
    18. ButtonWidget( int widgets );
    19. ~ButtonWidget();
    20. .........
    21. private:
    22. QGroupBox* comboBoxes();
    23. QGroupBox* spinBoxes( bool updateButton );
    24. QGroupBox* checkBoxes();
    25. QGroupBox* paretoBoxes();
    26. QGroupBox* markndiffBoxes();
    To copy to clipboard, switch view to plain text mode 

    I just know the initialization as below. What kind of initialization is it in button.cpp?
    Qt Code:
    1. class Something
    2. 02 {
    3. 03 private:
    4. 04 int m_nValue;
    5. 05 double m_dValue;
    6. 06 int *m_pnValue;
    7. 07
    8. 08 public:
    9. 09 Something()
    10. 10 {
    11. 11 m_nValue = 0;
    12. 12 m_dValue = 0.0;
    13. 13 m_pnValue = 0;
    14. 14 }
    15. 15 };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: what does the constructor do here?

    Its a constructor initialisation list, as described here: http://www.learncpp.com/cpp-tutorial...ization-lists/

  3. #3
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    4

    Default Re: what does the constructor do here?

    thank you for your reply. I have read that already. But here it seems has nothing to do with initialization. This if -condition makes me confused. Because usually we use only initialization in constructor. But why here use "if" in constructor? could someone give me another simpler example? Maybe it is helpful for me to understand it.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: what does the constructor do here?

    You can use whatever code you like in the constructor, in this case various widgets are added depending on the value to pass to the constructor.

    For example, if you pass ButtonWidget::ComboBoxes, then you get ComboBoxes, if you pass ( ButtonWidget::ComboBoxes | ButtonWidget::SpinBoxes) then you'll get boith ComboBoxes and SpinBoxes, etc.

    If you really wanted to you could write an entire application in your constructor, but its not best practice to do so.

  5. The following user says thank you to squidge for this useful post:

    LB4229 (21st June 2011)

  6. #5
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    4

    Default Re: what does the constructor do here?

    thank you. I thought the constructor would be used only for initialization.
    Your explanation enlightens me.

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: what does the constructor do here?

    The constructor is only used for initialisation in the ButtonWidget. The 'widget' parameter is used to drive how the ButtonWidget is initialised using other methods (comboBoxes() etc.) in the class.

  8. The following user says thank you to ChrisW67 for this useful post:

    LB4229 (22nd June 2011)

  9. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: what does the constructor do here?

    Added to what squidge said, constructor is basically a special function which is called (automatically, compiler generates the required code) when object is created, you can do all the things in it which you could do in normal function, added to normal function constructors can take initialization list. Also there types of constructors like default constructor, copy constructor etc. which are beyond the scope of this post.

  10. The following user says thank you to Santosh Reddy for this useful post:

    LB4229 (22nd June 2011)

  11. #8
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: what does the constructor do here?

    The parameter "int widgets" isn't really an int (number), it's a bit set. The user of the class would specify a parameter that is the bitwise OR of some subset of the "enum Widgets" values -- eg "ComboBoxes | CheckBoxes". This then tells the constructor which widgets to add to the layout.

  12. The following user says thank you to DanH for this useful post:

    LB4229 (22nd June 2011)

Similar Threads

  1. Replies: 13
    Last Post: 19th June 2011, 22:04
  2. class constructor
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2008, 19:25
  3. connect in constructor?
    By McKee in forum Qt Programming
    Replies: 4
    Last Post: 13th October 2007, 22:53
  4. QImage constructor
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 13:06
  5. What default constructor?
    By bitChanger in forum General Programming
    Replies: 5
    Last Post: 15th February 2006, 20:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.