Results 1 to 6 of 6

Thread: Help: Signals/slots system

  1. #1
    Join Date
    May 2009
    Posts
    21
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Help: Signals/slots system

    Hey, how do you connect a signal from a child widget with a slot from the parent widget?

    1) I have a MainWindow, and i want to connect the newAction (QAction)
    2) the central widget of my main window is a stackedWidget which contains the child widget i want to connect
    3) the child widget is a customWidget with a public slot, called foo()

    See the attached image...

    i tried with this from stackedWidget, which is between(child & parent) mainwindow-customwidget:
    connect(parentWidget()->newAction, SIGNAL(triggered()), customWidget, SLOT(foo()));

    i get an error saying that QWidget doesn't have a member called newAction()....

    So, in short how to connect a child's slot with a parent's signal?

    thanks
    Attached Images Attached Images
    Last edited by 0xl33t; 23rd July 2009 at 21:59.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help: Signals/slots system

    The error is pretty clear I think. QWidget doesn't have a member called newAction (be it a function or a variable).

    You can declare a new signal for your parent that you relay to the child:
    in the parent:
    Qt Code:
    1. connect(newAction, SIGNAL(triggered()), this, SIGNAL(newActionTriggered()));
    To copy to clipboard, switch view to plain text mode 
    and then in the intermediate:
    Qt Code:
    1. connect(parent(), SIGNAL(newActionTriggered()), child, SLOT(foo()));
    To copy to clipboard, switch view to plain text mode 

    If you have a clear shot at the child widget from the parent, you could do:
    Qt Code:
    1. connect(newAction, SIGNAL(triggered()), intermediate->child, SLOT(foo()));
    To copy to clipboard, switch view to plain text mode 

    Edit: typo.

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

    0xl33t (23rd July 2009)

  4. #3
    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: Help: Signals/slots system

    Or you can cast the parent widget to appropriate type (i.e. using qobject_cast) and make the connection directly.
    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.


  5. #4
    Join Date
    May 2009
    Posts
    21
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help: Signals/slots system

    @Franz, THANKS, i didn't consider that solution! Now it works! yay!

    Quote Originally Posted by wysota View Post
    Or you can cast the parent widget to appropriate type (i.e. using qobject_cast) and make the connection directly.
    I never used qobject_cast, to which type do i have to cast the parentWidget? Isn't it already QWidget? Or did i understand wrong?

  6. #5
    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: Help: Signals/slots system

    You have to cast it to your widget type - the one that has "newAction" defined, which is probably "MainWidget".
    Qt Code:
    1. MainWidget *mw = qobejct_cast<MainWidget*>(parentWidget());
    2. Q_ASSERT(mw!=0);
    3. connect(mw->newAction(), SIGNAL(triggered()), customWidget, SLOT(foo()));
    To copy to clipboard, switch view to plain text mode 
    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.


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

    0xl33t (24th July 2009)

  8. #6
    Join Date
    May 2009
    Posts
    21
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help: Signals/slots system

    Quote Originally Posted by wysota View Post
    You have to cast it to your widget type - the one that has "newAction" defined, which is probably "MainWidget".
    Qt Code:
    1. MainWidget *mw = qobejct_cast<MainWidget*>(parentWidget());
    2. Q_ASSERT(mw!=0);
    3. connect(mw->newAction(), SIGNAL(triggered()), customWidget, SLOT(foo()));
    To copy to clipboard, switch view to plain text mode 
    yes that works too!! Thanks! Now i can create connections without limits :P

    thx

Similar Threads

  1. How to get system drive?
    By Raccoon29 in forum General Programming
    Replies: 2
    Last Post: 23rd November 2008, 14:13
  2. Using QT for Digital TV menu system?
    By mittalpa in forum Newbie
    Replies: 4
    Last Post: 2nd July 2008, 19:23
  3. System environment variable
    By fahlen in forum Qt Programming
    Replies: 4
    Last Post: 27th November 2007, 19:02
  4. coordinating system
    By eleanor in forum Qt Programming
    Replies: 3
    Last Post: 7th November 2007, 20:57
  5. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15

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.