Results 1 to 4 of 4

Thread: pass parent parameter to base class constructor

  1. #1
    Join Date
    Mar 2014
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Question pass parent parameter to base class constructor

    Hi all,

    I'm studying this book "C++ GUI programming with Qt4" trying to learn it but my C++ was always rudamentary and even that is very rusty. I'm trying to make sure I understand it rather than just copy out examples. So in one of the examples there is this construct:

    Qt Code:
    1. FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
    To copy to clipboard, switch view to plain text mode 

    wich is described in the text this way:

    we pass on the parent parameter to the base class constructor
    then it's instantiated like this:

    Qt Code:
    1. FindDialog *dialog = new FindDialog;
    To copy to clipboard, switch view to plain text mode 

    ie. with no parameters.

    Can anybody explain what is going on here? Or where I can read up on it (in newbie speak)?
    I know it's sub-classing QDialog but what does 'parent' refer to? Where does it come from?

    Thanks.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pass parent parameter to base class constructor

    Something is a declaration :
    Qt Code:
    1. FindDialog::FindDialog(QWidget *parent = NULL);
    To copy to clipboard, switch view to plain text mode 
    I.e. parameter have a default value. You should first get to know C++.

  3. #3
    Join Date
    Mar 2014
    Posts
    15
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: pass parent parameter to base class constructor

    Thanks. I took some programming programs at night school years ago (BASIC, TurboPascal, C++, even a separate one for pointers) but never used it and, obviously, forgot most of it. Never even considered the default value - duh. So if I wanted to instantiate but with a parent I would do something like:

    Qt Code:
    1. FindDialog *dialog = new FindDialog(<pointer to parent widget>);
    To copy to clipboard, switch view to plain text mode 

    And, because this widget is to be the 'window' I can basically ignore the FindDialog parameter (so it gets the default of no parent)?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: pass parent parameter to base class constructor

    In most cases you could do something like this

    Qt Code:
    1. FindDialog *dialog = new FindDialog(this);
    To copy to clipboard, switch view to plain text mode 

    I.e. in most cases when you create a dialog you are inside the code of a widget.

    Cheers,
    _

Similar Threads

  1. Pass QString array to constructor
    By Ozzy in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2014, 20:36
  2. Replies: 11
    Last Post: 14th March 2010, 22:00
  3. Replies: 14
    Last Post: 1st December 2009, 20:45
  4. Replies: 4
    Last Post: 12th August 2008, 01:55
  5. Replies: 6
    Last Post: 4th April 2006, 07:13

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.