PDA

View Full Version : Reimplementing mouseMoveEvent function in QGraphicsProxyWidget does not work.



Binpix
30th August 2013, 02:10
Hello,
I am doing a program which allow to put, move and resize QWidget (QSlider, QPushButton, QDial...) inside a workspace. This workspace is a QGraphicView. I use a QGraphicsProxyWidget with a QGraphicsRectItem as parent to be able to move it easily and I give my widget to ProxyWidget which is inherited from QGraphicsProxyWidget.
Then, to resize my widget, I try to reimplement the functions mousePressEvent() (which works well) and mouseMoveEvent() but it does not work.

I tried to do setMouseTracking(true) on my widget, but nothing.

Here my code:



QGraphicsRectItem* rectItem = new QGraphicsRectItem();
rectItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
scene->addItem(rectItem);

QDial *label = new QDial();
label->setMouseTracking(true);
ProxyWidget *proxyWidget = new ProxyWidget(rectItem);
proxyWidget->setWidget(label);




void ProxyWidget::mousePressEvent(QGraphicsSceneMouseEv ent *e)
{
if (this->isMovable)
e->ignore(); //[1]
else
e->accept();
}


In ProxyWidget I have just reimplemented mousePressEvent() and mouseMoveEvent().

Any idea?

EDIT: If I change e->ignore() by e->accept() mouseMoveEvent() works. But I cannot move my widget anymore because my widget get the focus, for exemple for a QPushButton, the button is pushed.

wysota
30th August 2013, 07:45
EDIT: If I change e->ignore() by e->accept() mouseMoveEvent() works. But I cannot move my widget anymore because my widget get the focus, for exemple for a QPushButton, the button is pushed.

How would you define "works" and "does not work" in this case? To me it seems both your approaches "work", they just doesn't do what you want.

wagmare
30th August 2013, 08:17
im facing something similar ..

ProxyWidget::ProxyWidget(const QRectF &bounds, QGraphicsItem *parent)
: QGraphicsObject(parent), m_bounds(bounds)
{
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(this);
QDial *dial = new QDial();
dial->setFixedSize(m_bounds.width(), m_bounds.height());
dial->setFocusPolicy(Qt::StrongFocus);
dial->setMouseTracking(true);
dial->installEventFilter(this);
proxy->setWidget(dial);
proxy->setPos(0, 0);

}

bool
ProxyWidget::eventFilter(QObject *obj, QEvent *watched)
{
if (watched->type() == QEvent::MouseButtonPress )
{

qDebug()<<"Mouse Move Debug";
return true;

}
return true;
}

this not showing any dial view but if i remove installEventFilter then rest is fine ..

wysota
30th August 2013, 08:49
And how is that related to the OP's question? Which part exactly is "similar"? I cannot see any similarities.

wagmare
30th August 2013, 08:58
And how is that related to the OP's question? Which part exactly is "similar"? I cannot see any similarities.
i accepthat .. but why dial is not showing.. can u please tell me .. the same happens when i use QPushButton in place of QDial .. it will be always pressed ..

wysota
30th August 2013, 09:16
Please start your own thread instead of hijacking this one.

Binpix
30th August 2013, 14:39
How would you define "works" and "does not work" in this case? To me it seems both your approaches "work", they just doesn't do what you want.

You are right, both work but do not do what I want. If I do e->accept() in mousePressEvent(), mouseMoveEvent() is called in ProxyWidget but then the event is given to the widget owned by ProxyWidget. I would like to stop that because if the widget get the event I cannot move it.

I have found something:



void ProxyWidget::mouseMoveEvent(QGraphicsSceneMouseEve nt *e)
{
qDebug() << "mouseMoveEvent";

if (!this->isMovable) // If it is not in edtition mode
QGraphicsProxyWidget::mouseMoveEvent(e);
}


When I do that, the widget does not get the event when I am in edition mode, but the parent which is a QGraphicsRectItem is not movable anymore.

How can I do to give the event to QGraphicsProxyWidget::mouseMoveEvent(e) but telling it to do not give the event to the widget?

Do I have to reimplement the move behaviour?

wysota
30th August 2013, 15:01
You can always ignore the event in the widget.