Results 1 to 5 of 5

Thread: How to call a function in the mother object from a child object?

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to call a function in the mother object from a child object?

    Hello!

    Offently we create classes and instantiate objects from thoose classes in other objects of other classes (so in a Mother Object, a QMainWindow, I insert a Child Object, such as a QPushButton). Normally what is done is that in the Mother Object we work with the Child Object, using its functions. So for example:

    Qt Code:
    1. void MainWindow::useChildObject()
    2. {
    3. pb.setChecked(true);
    4. }
    To copy to clipboard, switch view to plain text mode 

    But how should I proceed if I want to use a function of the MOther Object from the Child Object? In other words, I'm there editting the .cpp of a QPushButton and in a moment I want to call a function of MainWindow from it. How should I do this?

    For now I'm using the SIgnals and Slots mechanism, and this have being fine till I find that there are some times when I need to get the result of a Main Object function (a return value), what can't be done by the Signals and Slots mechanism (as far as I know).

    So, how do I call a function of a Mother Object and even use its returns from a Child Object?


    Thanks!


    Momergil

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to call a function in the mother object from a child object?

    I'm there editting the .cpp of a QPushButton
    Don't do that, I'm 1000% sure its not needed. If you need a computation result in a button class, then it means that something is wrong with your design - a button is just a gui control, its only to process the user input, should be independent of the program logic.
    Post more details about your problem, I'm sure we can help you redesign the implementation.

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


  4. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to call a function in the mother object from a child object?

    Quote Originally Posted by stampede View Post
    Don't do that, I'm 1000% sure its not needed. If you need a computation result in a button class, then it means that something is wrong with your design - a button is just a gui control, its only to process the user input, should be independent of the program logic.
    Post more details about your problem, I'm sure we can help you redesign the implementation.
    Nonono! you misunderstood my point! xD

    The QPushButton was only an example!! It could be any object-class, lets suppose, one that I created. So don't concentrate in that being a QPushButton, but in being a class!!


    Momergil

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


  6. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to call a function in the mother object from a child object?

    Add a pointer of your parent class in your child class, change the constructors to pass the parent pointer. Once you have the parent pointer inside child you can easily access any public method of parent.

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


  8. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to call a function in the mother object from a child object?

    You can do this, but it makes the parent-child system highly coupled. This is not a good situation to be in as the design is not flexible, and is not modular. Now you cannot use your child component anywhere - it must be used with only specific parents.

    When this happens you should be looking at doing some dependency inversion
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. The following user says thank you to amleto for this useful post:


Similar Threads

  1. Function call from a NULL this object ?
    By Computer Hater in forum Qt Programming
    Replies: 3
    Last Post: 24th September 2011, 16:07
  2. cannot call member function without object
    By been_1990 in forum Qt Programming
    Replies: 11
    Last Post: 23rd October 2010, 17:12
  3. Replies: 2
    Last Post: 7th July 2009, 17:44
  4. Cannot call function without object
    By Salazaar in forum Newbie
    Replies: 5
    Last Post: 11th June 2007, 14:55
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

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.