Results 1 to 4 of 4

Thread: Suggestion on how to use SIGNAL & SLOT to connect 2 QGraphicObjects

  1. #1
    Join Date
    May 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Suggestion on how to use SIGNAL & SLOT to connect 2 QGraphicObjects

    Hi all,

    There is two GraphicObjects. Both have mousepressevent and also needed to be seen in a QGraphicsScene. The first GraphicObjects(let say A), when user clicks on it the second GraphicObjects(let say B) will be painted on the QGraphicScene while A still remain in the scene. But when user clicks on B the object A will be deleted together with B after being clicked. I use SIGNAL & SLOT to implement this which can only work for 1 pair. It became chaotic when there is plenty of object A in the scene. Please help me. I write down a pseudo code below.

    Qt Code:
    1. class A
    2. {
    3. paint()//paints the object
    4. mousePressEvent
    5. {
    6. emit drawBtn//emit signal
    7. }
    8. }
    9.  
    10. class B
    11. {
    12. mousePressEvent
    13. {
    14. emit DestroyA
    15. emit DestroySelf
    16. }
    17. }
    18.  
    19. class Widget
    20. {
    21. Widget()
    22. {
    23. scene = new QGraphicsScene
    24. view = new QGraphicsView
    25. createA();//this one only work once.
    26.  
    27. }
    28.  
    29. void createA()
    30. {
    31. //create classA and add to the scene
    32. connect(A, SIGNAL(drawBtn()),this,SLOT(drawTheBtn())
    33. }
    34. void drawTheBtn()
    35. {
    36. //create classB and add to the scene
    37. connect(B, SIGNAL(destroyA()),A,SLOT(deleteLater())
    38. connect(B, SIGNAL(destroySelf(),B,SLOT(deleteLater())
    39. }
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Suggestion on how to use SIGNAL & SLOT to connect 2 QGraphicObjects

    You can use the parent-child relation. If the parent of a QObject gets deleted, all children also get deleted.

  3. #3
    Join Date
    May 2011
    Posts
    18
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Suggestion on how to use SIGNAL & SLOT to connect 2 QGraphicObjects

    Yeah already attempted that method however that doesn't solve the goal. The goal is classB only appear into the scene when classA receives a click from the user.

    The goal is:
    A(appear in the scene)--->user clicks on A---->B appear(appear on the scene)
    ---->user clicks on B-----A & B destroy.

    Like I said previously I implemented with a different method and it works however only for one pair. Both also needed to be in the QGraphicsScene.

    The problem having parent-child relationship i can't seem to add both item in the scene(scene.addItem) but only in the constructor of class A. Please help me. Thanks.

  4. #4
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Suggestion on how to use SIGNAL & SLOT to connect 2 QGraphicObjects

    So the problem is this connect:
    Qt Code:
    1. connect(B, SIGNAL(destroyA()),A,SLOT(deleteLater())
    To copy to clipboard, switch view to plain text mode 
    You have n-number of As to which you would need to connect the B's signal?
    How about if you create an another class which knows all the As and it can delete them when necessary?
    Something like this:
    Qt Code:
    1. class Deleter {
    2. void addA( A *a ) {
    3. aList << a;
    4. }
    5. void deleteAs() {
    6. // loop through aList, delete each A-object and then clear the list itself
    7. }
    8. QList<A*> aList;
    9. }
    To copy to clipboard, switch view to plain text mode 
    When you create an A object you pass it to addA() and when B is clicked you call deleteAs().

Similar Threads

  1. Replies: 2
    Last Post: 15th September 2010, 00:54
  2. Can't connect a signal to a slot
    By cejohnsonsr in forum Newbie
    Replies: 5
    Last Post: 26th August 2010, 20:42
  3. How to connect signal/slot in QItemEditorFactory?
    By yyalli in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2010, 14:56
  4. problem connect signal - slot
    By jaca in forum Newbie
    Replies: 13
    Last Post: 9th March 2010, 19:38
  5. A signal/slot connect isn't working.
    By Daimonie in forum Qt Programming
    Replies: 6
    Last Post: 15th February 2009, 22:55

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.