PDA

View Full Version : Move QGraphicsProxyWidget



RThaden
25th February 2010, 13:21
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

RThaden
25th February 2010, 17:33
Hi all,

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



void ProxyWidget::mousePressEvent(QGraphicsSceneMouseEv ent *event)
{
QPointF pos = event->pos();
QPointer<QWidget> alienWidget = widget()->childAt(pos.toPoint());
if (qobject_cast<QPushButton*>(alienWidget))
{
QGraphicsProxyWidget::mousePressEvent(event);
grabbedByWidget=true;
}
else
{
QGraphicsItem::mousePressEvent(event);
grabbedByWidget=false;
}
}

void ProxyWidget::mouseReleaseEvent(QGraphicsSceneMouse Event *event)
{
if (grabbedByWidget)
QGraphicsProxyWidget::mouseReleaseEvent(event);
else
QGraphicsItem::mouseReleaseEvent(event);
grabbedByWidget=false;
}

void ProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEve nt *event)
{
if (grabbedByWidget)
return;
QGraphicsItem::mouseMoveEvent(event);
}



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

aamer4yu
25th February 2010, 18:15
Normally a graphics item is movable if u set the flags properly. Why did you need to re implement the mouse events?

RThaden
25th February 2010, 18:26
It is a QGraphicsProxyWidget, not a QGraphicsItem

aamer4yu
25th February 2010, 18:53
And isnt QGraphicsProxyWidget a descendant of QGraphicsItem :rolleyes:

RThaden
26th February 2010, 09:04
Yes it is. But derived classes sometimes override implementations :rolleyes:
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.

wagmare
26th February 2010, 09:20
little suggestion ...
add this QGraphicsProxyWidget to a QGraphicsRectItem as parent .... just like padnavigator example's roundrectitem.cpp ... u canmove the RoundRectItem ...

RThaden
1st March 2010, 10:30
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

meazza
1st April 2011, 11:30
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

RThaden
1st April 2011, 11:45
meazza,

you see my solution. I posted the code. That worked.
I derived from QGraphicsProxyWidget and in the constructor, I did


setFlag(ItemIsMovable);
setFlag(ItemIsSelectable);


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

Regards,

Rainer

meazza
1st April 2011, 12:21
MyProxy::MyProxy(QGraphicsProxyWidget *parent) :
QGraphicsProxyWidget(parent)
{

setFlag(ItemIsMovable);
setFlag(ItemIsSelectable);
}
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
QGraphicsItem::mousePressEvent(mouseEvent);
}


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?

RThaden
1st April 2011, 12:26
Did you do like this


#include <QGraphicsItem>

class MyProxy : public QGraphicsProxyWidget
{
Q_OBJECT
...
}


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

Bilderbikkel
12th November 2012, 21:00
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/CppQGraphicsProxyWidgetExample6.htm

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

Cheers, Bilderbikkel

RThaden
13th November 2012, 09:29
Wow, quite some time ago.
I had a look at your example. Why do you use boost and Qt signals?

Best regards,

Rainer

Dariusz
17th September 2016, 14:01
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.