Results 1 to 15 of 15

Thread: Move QGraphicsProxyWidget

  1. #1
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Move QGraphicsProxyWidget

    Hi all,

    I didn't find a solution for my problem up to now:
    I have a derived class from QGraphicsProxyWidgets and want to move these in a QGraphicsScene.What I did up to now was reimplementing mousePressEvent, mouseReleaseEvent and mouseMoveEvent and called the implementations of QGraphicsItem for these events. That worked fine, but now I have a problem since I added a QPushButton in my widget which resides in the proxy widget and the mousePressEvents are not forwarded.
    So, I removed my own implementations of the mouse event handlers with the result that I can use the QPushButton normally but lost the ability to move the widgets.
    I'd like to have both.

    Any suggestions what I have to do?
    Reimplement the move functionality with itemChanged and setPos?
    Or forware mouseClicks to the widgets?

    Best regards,

    Rainer

  2. #2
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Move QGraphicsProxyWidget

    Hi all,

    I found a solution. Seems a bit hackish to me, am thankful for an opinion:

    Qt Code:
    1. void ProxyWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. QPointF pos = event->pos();
    4. QPointer<QWidget> alienWidget = widget()->childAt(pos.toPoint());
    5. if (qobject_cast<QPushButton*>(alienWidget))
    6. {
    7. QGraphicsProxyWidget::mousePressEvent(event);
    8. grabbedByWidget=true;
    9. }
    10. else
    11. {
    12. QGraphicsItem::mousePressEvent(event);
    13. grabbedByWidget=false;
    14. }
    15. }
    16.  
    17. void ProxyWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    18. {
    19. if (grabbedByWidget)
    20. QGraphicsProxyWidget::mouseReleaseEvent(event);
    21. else
    22. QGraphicsItem::mouseReleaseEvent(event);
    23. grabbedByWidget=false;
    24. }
    25.  
    26. void ProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    27. {
    28. if (grabbedByWidget)
    29. return;
    30. QGraphicsItem::mouseMoveEvent(event);
    31. }
    To copy to clipboard, switch view to plain text mode 

    That is, I call either the implementations for the mouse event handlers of QGraphicsItem or QGraphicsProxyWidget depending on the mouse position. If I find a QPushButton under the mouse, I call the widgets handler.

    Regards,

    Rainer

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move QGraphicsProxyWidget

    Normally a graphics item is movable if u set the flags properly. Why did you need to re implement the mouse events?

  4. #4
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Move QGraphicsProxyWidget

    It is a QGraphicsProxyWidget, not a QGraphicsItem

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move QGraphicsProxyWidget

    And isnt QGraphicsProxyWidget a descendant of QGraphicsItem

  6. #6
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Move QGraphicsProxyWidget

    Yes it is. But derived classes sometimes override implementations
    QGraphicsProxyWidget has own implementations for the mouse event handlers that do not support moving it by default.
    Instead they have to care for the contained QWidget to get the mouse events.
    Last edited by RThaden; 26th February 2010 at 10:21.

  7. #7
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move QGraphicsProxyWidget

    little suggestion ...
    add this QGraphicsProxyWidget to a QGraphicsRectItem as parent .... just like padnavigator example's roundrectitem.cpp ... u canmove the RoundRectItem ...
    "Behind every great fortune lies a crime" - Balzac

  8. The following user says thank you to wagmare for this useful post:

    RThaden (1st March 2010)

  9. #8
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Move QGraphicsProxyWidget

    Thanks wagmare for this helpful suggestion. I will try that and report the results, however it will take some time, since my approach works (but smells a bit) and so the priority is a bit low at the moment.

    Best regards,

    Rainer

  10. #9
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Move QGraphicsProxyWidget

    How exactly did you accomplish this? I have a similar problem, but I cant move my proxywidgets at all in the scene. Been trying all morning but with no success. So I am hoping that you can give me some pointers.

    I have also made a class deriving from QGraphicsProxyWidget but it wont allow me to pass the QGraphicsItem events

  11. #10
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Move QGraphicsProxyWidget

    meazza,

    you see my solution. I posted the code. That worked.
    I derived from QGraphicsProxyWidget and in the constructor, I did
    Qt Code:
    1. setFlag(ItemIsMovable);
    2. setFlag(ItemIsSelectable);
    To copy to clipboard, switch view to plain text mode 

    It's some time ago and I don't remember all the details.

    Regards,

    Rainer

  12. #11
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Move QGraphicsProxyWidget

    Qt Code:
    1. MyProxy::MyProxy(QGraphicsProxyWidget *parent) :
    2. QGraphicsProxyWidget(parent)
    3. {
    4.  
    5. setFlag(ItemIsMovable);
    6. setFlag(ItemIsSelectable);
    7. }
    8. void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
    9. {
    10. QGraphicsItem::mousePressEvent(mouseEvent);
    11. }
    To copy to clipboard, switch view to plain text mode 

    I just created the class again from scratch. And this code gives me an error that i cant call that function without an object.


    Added after 17 minutes:


    Solved it, just some syntax error. But now when I move an item the proxy widget moves also like they are linked... Anyone got any ideas?
    Last edited by meazza; 1st April 2011 at 13:21.

  13. #12
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Move QGraphicsProxyWidget

    Did you do like this

    Qt Code:
    1. #include <QGraphicsItem>
    2.  
    3. class MyProxy : public QGraphicsProxyWidget
    4. {
    5. Q_OBJECT
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    I have no idea, what exactly causes the error. Sounds like something basically is wrong. Also, I don't see the exact error message.
    For me, it works fine.

    Regards,

    Rainer

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

    meazza (1st April 2011)

  15. #13
    Join Date
    Feb 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Move QGraphicsProxyWidget

    After reading the many suggestions from this thread, I still had to struggle a lot to get a complete and working example. Additionally, I took a very different approach: the QGraphicsProxyWidget signals that it receives a mouse click and follows the mouse cursor until it signals a mouse release event. Next to this, the widget signals when it wants to lose focus (and has to try hard to really do so).

    Code can be found here: http://richelbilderbeek.nl/CppQGraph...etExample6.htm

    I hope it helps you guys out as much as it helped me!

    Cheers, Bilderbikkel
    Last edited by Bilderbikkel; 12th November 2012 at 22:01. Reason: Grammar error

  16. #14
    Join Date
    Jan 2008
    Posts
    56
    Thanks
    7
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Move QGraphicsProxyWidget

    Wow, quite some time ago.
    I had a look at your example. Why do you use boost and Qt signals?

    Best regards,

    Rainer

  17. #15
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    1
    Qt products

    Default Re: Move QGraphicsProxyWidget

    I've been trying to also be able to move QGraphicsProxyWidget but in pyqt python. Any chance one of you can translate the code to python? I'm struggling with some parts and thus cant get it to work at all.

Similar Threads

  1. help positioning QGraphicsProxyWidget?
    By Scott Shiff in forum Qt Programming
    Replies: 1
    Last Post: 25th January 2010, 00:13
  2. QGraphicsItem or QGraphicsProxyWidget?
    By ttvo in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2009, 00:13
  3. QGraphicsProxyWidget
    By QbelcorT in forum Qt Programming
    Replies: 0
    Last Post: 29th November 2008, 09:21
  4. problem with QGraphicsProxyWidget
    By dreamer in forum Qt Programming
    Replies: 5
    Last Post: 19th May 2008, 23:25
  5. Move Rectangle on mouse Move
    By vermarajeev in forum Qt Programming
    Replies: 24
    Last Post: 14th May 2007, 06:34

Tags for this Thread

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.