Results 1 to 18 of 18

Thread: Connecting child and parent

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    40
    Thanked 8 Times in 4 Posts

    Default Connecting child and parent

    Hi gusy,

    I have a strange problem.
    I have 2 classes:

    1) ClassOne : public QWidget {;}
    2) ClassTwo : public QGraphicsScene {;} // call it objectTwo

    I need to connect an action from the QGraphicsScene Context menu to a slot from the ClassOne(which is set as parent for the ClassTwo).

    I tried both:

    in the ClassOne:

    connect(objectTwo->m_action,SIGNAL(triggered()),this,SLOT(vslot()) );

    in the ClassTwo:

    connect(m_action,SIGNAL(triggered()),this->parent(),SLOT(vslot()));

    Both do not work: nothing happens or there are run-time bugs.

    Any idea.

    Could you give a simple example how to connect child's action to it's parent slot?

    Thanks.

    Maverick
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 21 Times in 21 Posts

    Default Re: Connecting child and parent

    Did you ensure that the macro Q_OBJECT is declared in both classes ?

    If so, are your slots methods declared as slots ?

  3. #3
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    40
    Thanked 8 Times in 4 Posts

    Default Re: Connecting child and parent

    Yes. I have declared the Q_Object macros + declared slots as...slots

    Maverick
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  4. #4
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 21 Times in 21 Posts

    Default Re: Connecting child and parent

    Is your code small enough to post it here, so I can test ?

  5. #5
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    40
    Thanked 8 Times in 4 Posts

    Default Re: Connecting child and parent

    /// After running the application when I try to trigger the action I got a breakpoint

    .....
    if (!check_signal_macro(sender, signal, "connect", "bind"))
    return false;
    const QMetaObject *smeta = sender->metaObject(); // Here Is the breakpoint and the problem
    ++signal; //skip code
    int signal_index = smeta->indexOfSignal(signal);
    if (signal_index < 0) {
    // check for normalized signatures
    ....
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  6. #6
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    40
    Thanked 8 Times in 4 Posts

    Default Re: Connecting child and parent

    Qt Code:
    1. class MyScene: public QGraphicsScene
    2. {
    3. Q_OBJECT
    4. Q_CLASSINFO("version", "1.0.0");
    5.  
    6. public:
    7. MyScene(VPFPrimitives* primitives, QObject* parent = 0);
    8. ~MyScene() {}
    9.  
    10. protected:
    11.  
    12. void contextMenuEvent( QGraphicsSceneContextMenuEvent * contextMenuEvent);
    13. void vCreateContextMenuConnections();
    14.  
    15. private:
    16. VPFPrimitives* m_VPFPrimitives;
    17. QMenu* m_contextMenu;
    18. QAction* m_actionContextZI;
    19.  
    20. friend class FundGC;
    21. };
    22. class GC: public QWidget
    23. {
    24. Q_OBJECT
    25. Q_CLASSINFO("version", "1.0.0");
    26.  
    27. public :
    28. GC(QGraphicsView*,QWidget* parent = 0);
    29. ~GC();
    30. private:
    31. MyScene* m_scene;
    32. VPFPrimitives* m_VPFPrimitives;
    33. private slots:
    34. void vZoom();
    35.  
    36. };
    To copy to clipboard, switch view to plain text mode 


    And the implementation:

    Qt Code:
    1. MyScene::MyScene(VPFPrimitives* primitives,QObject* parent): QGraphicsScene(parent)
    2. {
    3. m_VPFPrimitives = primitives;
    4. vCreateContextMenuConnections();
    5. }
    6. /*! Creating context menu for the graphics Scene*/
    7. void MyScene::contextMenuEvent( QGraphicsSceneContextMenuEvent * contextMenuEvent)
    8. {
    9. m_contextMenu = new QMenu();/*!< Context Menu */
    10. m_actionContextZI = new QAction("Zoom In",this);
    11. m_contextMenu->addAction(m_actionContextZI);
    12. m_contextMenu->exec(contextMenuEvent->screenPos());
    13.  
    14. }
    15.  
    16. void MyScene::vCreateContextMenuConnections()
    17. {
    18. connect(m_actionContextZI,SIGNAL(triggered()),this->parent(),SLOT(this->parent()->vZoom()));
    19. }
    20. void GC::vZoom()
    21. {
    22. QMessageBox::warning(this,"yes","working");
    23. }
    To copy to clipboard, switch view to plain text mode 
    The code is quite long; I have pasted the most important parts
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  7. #7
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 21 Times in 21 Posts

    Default Re: Connecting child and parent

    It won't work since your slot is private..make it public (I think) !

  8. #8
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    40
    Thanked 8 Times in 4 Posts

    Default Re: Connecting child and parent

    I have made the slot public, but still it does not work.

    Maverick
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

  9. #9
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 21 Times in 21 Posts

    Default Re: Connecting child and parent

    Do you have some warnings like "No such slots..." ?

    Your parent object is a QObject, but you may have to cast it to GC object...

    Qt Code:
    1. GC *wparent = (GC*)(this->parent());
    To copy to clipboard, switch view to plain text mode 

    And use wparent in your connect.

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Connecting child and parent

    There is a logical mistake; m_actionContextZI does not exist by the time vCreateContextMenuConnections() gets called. Also, you might want to allocate the menu on the stack or create it only once (preferable with a parent).
    J-P Nurmi

  11. #11
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 21 Times in 21 Posts

    Default Re: Connecting child and parent

    Oh yes, right, you got it jpn

  12. #12
    Join Date
    May 2007
    Location
    Lublin, Poland
    Posts
    345
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    40
    Thanked 8 Times in 4 Posts

    Default Re: Connecting child and parent

    Good idea, I have changed it before, but now I have a different problem(also connected with this task). My signal/slot are connected. I test it using a messageBox. WHen I trigger the action for the first time nothing happens. When I do it the second time 2 messageboxes show, when I trigger it the third time, 3 mb appear...etc.


    Any idea

    Maverick
    Qt allows you to use everything you want
    wysota
    --------------------------------------------------------------------------------
    #if defined(Q_OS_UNIX) && defined(QT_DEBUG)
    abort(); // trap; generates core dump
    #else
    exit(1); // goodbye cruel world
    #endif

Similar Threads

  1. QProcess : child process listening parent output ?
    By Nyphel in forum Qt Programming
    Replies: 16
    Last Post: 20th March 2007, 13:40
  2. initialize child widgets within parent?
    By ucomesdag in forum Newbie
    Replies: 6
    Last Post: 6th June 2006, 08:11
  3. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00
  4. Infinite loop - resize parent from child
    By bitChanger in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2006, 13:21
  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
  •  
Qt is a trademark of The Qt Company.