Results 1 to 18 of 18

Thread: Connecting child and parent

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

    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
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    40
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    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
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    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
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Connecting child and parent

    Oh yes, right, you got it jpn

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

    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

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

    Default Re: Connecting child and parent

    Seems like multiple connect definitions...
    You must make sure that connect is called once only at initialization.

    Try to call disconnect after your context menu has been executed.

  14. The following user says thank you to guilugi for this useful post:

    maverick_pol (16th July 2007)

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

    Default Re: Connecting child and parent

    Here is the final version and the correct one(not the only solution)
    Qt Code:
    1. void GScene::contextMenuEvent( QGraphicsSceneContextMenuEvent * contextMenuEvent)
    2. {
    3. m_contextMenu = new QMenu();
    4. m_actionContextZI = new QAction("Zoom In",this);
    5. m_contextMenu->addAction(m_actionContextZI);
    6. connect(m_actionContextZI,SIGNAL(triggered()),this->parent()>parent(),SLOT(vZoomIn())); // this line can't be the last
    7. m_contextMenu->exec(contextMenuEvent->screenPos());
    8. }
    To copy to clipboard, switch view to plain text mode 

    THanks guys for your help.

    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

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

    Default Re: Connecting child and parent

    Sorry, but I'm not exactly sure about "correct". Notice that there is a memory leak. A new menu is being created every time a context menu is requested but never freed.
    J-P Nurmi

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

    Default Re: Connecting child and parent

    Yes, you're right.

    QMenu* m_contextMenu = new QMenu();/*! QAction* m_actionContextZI = new QAction("Zoom In",this);
    m_contextMenu->addAction(m_actionContextZI);
    connect(m_actionContextZI,SIGNAL(triggered()),this->parent()>parent(),SLOT(vZoomIn()));
    m_contextMenu->exec(contextMenuEvent->screenPos());


    Is this good?

    Thanks
    Last edited by maverick_pol; 16th July 2007 at 17:07.
    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

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

    Default Re: Connecting child and parent

    Well, a new menu is still created again and again upon every context menu request. All previously created menus remain alive as hidden eating up memory more and more.

    Actually I meant something like this:
    Qt Code:
    1. void GScene::contextMenuEvent( QGraphicsSceneContextMenuEvent * event )
    2. {
    3. QMenu menu(event->widget());
    4. menu.addAction("Zoom In", receiver, SLOT(zoom()));
    5. menu.exec(event->screenPos()); // QMenu::exec() blocks
    6. } // a local variable "QMenu menu" gets automatically destructed
    To copy to clipboard, switch view to plain text mode 

    Or alternatively something like this:
    Qt Code:
    1. GScene::GScene()
    2. {
    3. // ...
    4. // a method that creates the menu and establishes connections _once_
    5. createContextMenu();
    6. }
    7.  
    8. void GScene::contextMenuEvent( QGraphicsSceneContextMenuEvent * event )
    9. {
    10. m_contextMenu->exec(event->screenPos());
    11. }
    To copy to clipboard, switch view to plain text mode 

    However, if a single action context menu is all you need, using Qt::ActionsContextMenu is the simplest way:
    Qt Code:
    1. graphicsView->setContextMenuPolicy(Qt::ActionsContextMenu);
    2. graphicsView->addAction(m_actionContextZI);
    To copy to clipboard, switch view to plain text mode 

    Oh, and by the way, I suggest searching the forums for "signal chaining". It might be a bit more elegant and reliable way to establish connections instead of writing "parent()->parent()".
    J-P Nurmi

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

    Default Re: Connecting child and parent

    Thanks for advice.

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.