Results 1 to 5 of 5

Thread: Access parent's function from child widget

  1. #1
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Access parent's function from child widget

    I want to access a function in my parent from a child widget.

    This is the code of the parent widget:

    mainwindow.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <iostream>
    3. #include "mainwindow.h"
    4. #include "menu1.h"
    5.  
    6.  
    7.  
    8. mainwindow::mainwindow()
    9. {
    10. men = new menu1(this); // This is the child widget
    11.  
    12. qs = new QStackedWidget(this);
    13. qs->addWidget(men);
    14. qs->setCurrentIndex(0);
    15. }
    16.  
    17. mainwindow::functions1()
    18. {
    19. std::cout<<"function called";
    20. }
    To copy to clipboard, switch view to plain text mode 

    Here is the code of the child widget.

    menu1.cpp
    Qt Code:
    1. menu1::menu1(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4.  
    5. resize(880,558);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    Now, I want to access funtion1 from 'men'. How do I do it?

    I tried the following in men.cpp but got errors:

    1) parent->funtion1();

    This gives the error: 'class Qwidget' has no member named 'funtion1'

    2) parent->parent->funtion1();

    This gave the error : invalid use of member (did you forget the '&')
    base operand of '->' is not a pointer


    Any help would be appreciated.

    Regards,

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Access parent's function from child widget

    Either cast the parent pointer to the right type or store the pointer to the parent somewhere in the child when receiving it in the constructor. Or use signals and slots.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Access parent's function from child widget

    Quote Originally Posted by wysota View Post
    Either cast the parent pointer to the right type or store the pointer to the parent somewhere in the child when receiving it in the constructor. Or use signals and slots.
    Ok, I'll try the casting.

    But why is 'parent()' not working? Isin't it doing the same thing (pass the parent pointer) ?

  4. #4
    Join Date
    Feb 2011
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Access parent's function from child widget

    Quote Originally Posted by el33t View Post
    Ok, I'll try the casting.

    But why is 'parent()' not working? Isin't it doing the same thing (pass the parent pointer) ?
    The return value of `parent()` is `QObject*` by default, not `mainwindow*`. As such, you do not have access to `mainwindow` functions, even though you're pretty sure that's what type of object you actually have. That's why the cast is neccessary; it tells the compiler that even though you have a `QObject*`, it should be treated as a `mainwindow*` and let you call those functions as normal.

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

    el33t (16th March 2011)

  6. #5
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Access parent's function from child widget

    Did the casting an everything worked like a charm. Thanks a lot for the assitance guys!

Similar Threads

  1. Replies: 1
    Last Post: 11th March 2011, 19:34
  2. Replies: 7
    Last Post: 14th January 2010, 08:47
  3. Replies: 4
    Last Post: 3rd October 2009, 08:19
  4. Child-Parent Widget??
    By anupamgee in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2009, 13:17
  5. Referencing Parent Widget from Child
    By taylor34 in forum Qt Programming
    Replies: 8
    Last Post: 11th April 2006, 15:13

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.