Results 1 to 4 of 4

Thread: confusion with a STATEMENT used frequently

  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default confusion with a STATEMENT used frequently

    Hi,
    May be the question ahead also comes in domain of Object Oriented ,i am confused with

    Qt Code:
    1. class exclass :public QObject, public QGraphicsItem
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. exclass(QGraphicsItem *parent = 0);
    7. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "exclass.h"
    2.  
    3. exclass::exclass(QGraphicsItem *parent): QGraphicsItem(parent)
    4. {
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "exclass.h"
    2.  
    3. #include <QApplication>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. exclass *mainScene = new exclass;
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    I am confused with the statement

    'exclass(QGraphicsItem *parent = 0); ' in the .h file and

    'exclass::exclass(QGraphicsItem *parent): QGraphicsItem(parent){}' in the .cpp file

    can anyone explain why the declaration as 'QGraphicsItem *parent' is necessary in the exclass constructor and why do we say it as 'QGraphicsItem *parent = 0 'in the .h file..
    i avoided this question so far and just followed it but now i have to develop a QGraphicsItem parent and a child so i am confused with how to do it..

  2. #2
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: confusion with a STATEMENT used frequently

    Not sure how much you do or don't understand, but here are some ideas:

    The parent/child object relationship metaphor is an intuitve way to build complex systems in a GUI.

    In the header file, when you see something like, QGraphicsItem * parent = 0, that is referred to as a default parameter. It means that it can accept the parameter, but if you don't provide one, there is a default value to be passed in - in this case 0, or nothing, no parent.

    //no parent
    QGraphicsItem * parent_item = new QGraphicsItem();

    //has a parent
    QGraphicsItem * child_item = new QGraphicsItem(parent_item);

    In the constructor implementation, because you are subclassing a QGraphicsItem, if your class ends up with a parent, then you pass it up to the base class, that is what you are doing here:

    exclass::exclass(QGraphicsItem * parent): QGraphicsItem(parent){}

  3. #3
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: confusion with a STATEMENT used frequently

    Thanks for the reply,but can you please elobrate more on this part :

    " In the constructor implementation, because you are subclassing a QGraphicsItem,
    if your class ends up with a parent, then you pass it up to the base class, that is what you are doing here:

    exclass::exclass(QGraphicsItem * parent): QGraphicsItem(parent){}
    "

  4. #4
    Join Date
    Sep 2007
    Location
    Rome, GA
    Posts
    199
    Thanks
    14
    Thanked 41 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: confusion with a STATEMENT used frequently

    Someone with more experience may need to step in to clarify, but the parent/child relationship of objects in Qt has to do with various things, one big thing being that the parent object controls when to destroy its children. If you subclass a Qt object, and it has a parent, then you need to pass that parent object to the base class as well, so it can call the right destructors up the chain. There may be other reasons too, and what I said may not be entirely accurate. Someone correct me if I'm wrong.

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.