Results 1 to 6 of 6

Thread: Someone please help me understand the syntax.

  1. #1
    Join Date
    Oct 2015
    Posts
    35
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    11

    Default Someone please help me understand the syntax.

    Someone please help me understand the syntax and what is happening in these below lines of the code from the link http://ldc.usb.ve/docs/qt/tutorial-t5.html. Thanks in advance.

    class MyWidget : public QWidget
    {
    public:
    MyWidget(QWidget *parent = 0); <== is MyWidget a class a function or a constructor?
    };

    MyWidget::MyWidget(QWidget *parent): QWidget(parent) <== What is this syntax and what does it mean?
    Last edited by rookee; 23rd October 2015 at 19:22.

  2. #2
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11
    Thanks
    43
    Thanked 1 Time in 1 Post

    Default Re: Someone please help me understand the syntax.

    MyWidget(QWidget *parent = 0); <== is MyWidget a class a function or a constructor?
    That is the declaration of the constructor

    MyWidget::MyWidget(QWidget *parent): QWidget(parent) <== What is this syntax and what does it mean?
    Past the colon is the initialization list. You are passing the parent to the base class with QWidget(parent)

  3. The following user says thank you to ravas for this useful post:

    rookee (28th October 2015)

  4. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Re: Someone please help me understand the syntax.

    Aye, aye ... I don't want to be rude but you should know that Qt is a set of C++ libraries, rookee. The meaning of the "things" above is a very basic C++. The first "thing" is a declaration of a constructor in a header, the second one is a code of the constructor specifying a constructor of the base class which should be called and how. Please, look at some C++ book, you cannot pass without a basic knowledge of C++. In fact, you will need considerably more than a basic knowledge.

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

    rookee (28th October 2015)

  6. #4
    Join Date
    Sep 2012
    Location
    Iran
    Posts
    34
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    33
    Thanked 2 Times in 2 Posts

    Default Re: Someone please help me understand the syntax.

    Quote Originally Posted by rookee View Post
    Someone please help me understand the syntax and what is happening in these below lines of the code from the link http://ldc.usb.ve/docs/qt/tutorial-t5.html. Thanks in advance.

    class MyWidget : public QWidget
    {
    public:
    MyWidget(QWidget *parent = 0); <== is MyWidget a class a function or a constructor?
    };

    MyWidget::MyWidget(QWidget *parent): QWidget(parent) <== What is this syntax and what does it mean?
    If you review the chapters explaining classes in c++, you will know everything here.
    basically
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5.  
    6. };
    To copy to clipboard, switch view to plain text mode 
    you have a class named MyWidget, which inherits from QWidget. so when you do create your classes, most of the time you also define a constructor for it.
    and the constructor has the name of your class, without any return type, It can take any number of parameters, including none!
    but since you are inheriting from something else, in order to get things working, if the base class needs something , you need to give what it needs
    and here we get it from the user and passes it to the base class's constructor.
    thats why we have a QWidgets* parameter for our constructor,
    Now we could also have the definition written here as well, which would look like this :
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0) :QWidget(parent)
    5. {
    6. ....
    7. }
    8.  
    9. };
    To copy to clipboard, switch view to plain text mode 
    This is kind of equivalent to
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0)
    5. {
    6. QWidget(parent)
    7. }
    8. };
    To copy to clipboard, switch view to plain text mode 

    And what is trying to say is that, first build the base part, gives it what ever it needs to properly initialize, and then continue with my specific customization.
    (and by the way, the second snippet which I gave you is not valid, I just wrote it as a hint of what happens here , you need to use the first style for initialing the base constructors )

    and finally, it is good programming practice to separate the class signature definition and their implementation .
    so to do that we can also do it like this :
    this is class definition (signature if you will )
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5.  
    6. };
    To copy to clipboard, switch view to plain text mode 

    and this is the implementation details
    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent): QWidget(parent)
    2. {}
    To copy to clipboard, switch view to plain text mode 
    the first part says, the remaining belongs to the MyWidget class

    you can see two simple example of both cases here :
    http://ideone.com/4XXZJw
    http://ideone.com/B4CtyX

    Hope this helps

  7. The following user says thank you to Hossein for this useful post:

    rookee (28th October 2015)

  8. #5
    Join Date
    Oct 2015
    Posts
    35
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    11

    Default Re: Someone please help me understand the syntax.

    @Radek: I've never done programming before, sorry for my dumb questions.

    Thank you all for your valuable time. So kind of you all.

  9. #6
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11
    Thanks
    43
    Thanked 1 Time in 1 Post

Similar Threads

  1. Something i am not understand about Qt.
    By HeX0R in forum Newbie
    Replies: 8
    Last Post: 12th February 2010, 04:37
  2. I don't quite understand this multithread example
    By HelloDan in forum Qt Programming
    Replies: 2
    Last Post: 9th April 2009, 08:58
  3. need help .not able to understand.......
    By sh123 in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2009, 01:50
  4. Replies: 6
    Last Post: 25th February 2008, 10:52
  5. don't understand Qt's setCodecForCStrings
    By ber_44 in forum Qt Programming
    Replies: 1
    Last Post: 9th July 2007, 01:19

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
  •  
Qt is a trademark of The Qt Company.