PDA

View Full Version : Change the cursor of a widget inside a QGraphicsProxyWidget



Paolo_BE
23rd March 2012, 09:44
Hi everyone,

I have got a problem to change the cursor shape of my widget on a left click in it.

Context (Qt 4.8, win 7):
I have a QGraphicsScene containing a QGraphicsProxyWidget. This proxy hold my widget.


QGraphicsScene scene;
QGraphicsProxyWidget proxy;
MyWidget widget;
proxy.setWidget(aWidget);
scene.addItem(proxy);


In MyWidget class, I have override the function mousepress & mouserelease as :



void MyWidget::mousePressEvent(QMouseEvent *ev){
if(ev->button() == Qt::LeftButton){
setCursor(QCursor(Qt::OpenHandCursor));
}
}

void MyWidget::mouseReleaseEvent(QMouseEvent *ev){

}



For information, MyWidget override also the repaintEvent


void MyWidget::paintEvent(QPaintEvent* p){
QPainter painter(this);
painter.drawPixmap(0,0,pp);
}


The problem is the mouse cursor only change when I press the left button of the mouse in the widget and I move my mouse out of the widget and go back again in it focus.

I have tested my code by showing MyWidget as a classic window (MyWidget::show()) and the cursor immediately change when I press the left mouse button.


I certainly miss something to add when the widget is in a proxy.

Thanks for your help,

Paolo

Paolo_BE
23rd March 2012, 13:39
Not the best way for sur, but a solution is to have a mapping beetween the widget and the proxy, we will call the proxy "parent"

the code will be something like :



void MyWidget::mousePressEvent(QMouseEvent *ev){
if(ev->button() == Qt::LeftButton){
//parent is the proxy containing MyWidget
parent->setCursor(QCursor(Qt::OpenHandCursor));
}
}

void MyWidget::mouseReleaseEvent(QMouseEvent *ev){
parent->setCursor(QCursor(Qt::ArrowCursor));
}



I hope that will help someone.

Paolo