PDA

View Full Version : Can you share an event with two widget?



rakefet
30th April 2014, 17:17
Hi,

I have two Graphicsview widgets that I display two images on them:
ui->widget1->Scene->addPixmap(pix1);
ui->widget2->Scene->addPixmap(pix2);
In my graphicsview class I created a mouse wheelEvent that zoom the image in the graphicsview.
I would like to be able to use my mouse wheel on one of the graphicsview widget and both images in widget1 and widget2 will zoom at the same time. Is it possible?

Thanks in advance.

anda_skoa
30th April 2014, 20:15
If it is a viewport transformation then you can apply the same transformation on both views.
Same for scene or item transformations.

You can also use the same scene in two views if you like

Cheers,
_

rakefet
1st May 2014, 02:32
I am not sure I understand. The zooming event is associated to the widget where the mouse event occurs. How do I access the other widget ? ui is not recognized in this scope.
The only way I can think is to emit a signal in the mouse wheel event which will be received by slots at the two widget.
Can you be more specific in your answer?

Thanks

anda_skoa
1st May 2014, 13:15
A cross widget signal/slot connection sounds good.

Cheers,
_

rakefet
4th May 2014, 15:48
Hi,

Zooming using signal/slot connection is working fine. When I try to do the same thing with panning I am getting the error:

"invalid use of incomplete type class QScrollBar ...hBar->setValue()..."

My code is:


void MyGraphicsView::mousePressEvent(QMouseEvent * e)
{
if (e->buttons().testFlag(Qt::MidButton))
{
.
.
.
}
if (e->buttons().testFlag(Qt::LeftButton))
{
_lastPos = e->pos();
QGraphicsView::mousePressEvent(e);
}
}
void MyGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons().testFlag(Qt::LeftButton))
{
QPoint delta = event->pos() - _lastPos;
_lastPos = event->pos();
emit panning_signal(delta);
}
QGraphicsView::mouseMoveEvent(event);
}

QObject::connect(ui->widget1, SIGNAL(panning_signal(QPoint )), this, SLOT(panning(QPoint )));
QObject::connect(ui->widget2, SIGNAL(panning_signal(QPoint )), this, SLOT(panning(QPoint )));
void MainWindow::panning(QPoint delta)
{
QScrollBar *hBar = ui->widget1->horizontalScrollBar();
QScrollBar *vBar = ui->widget1->verticalScrollBar();

hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
vBar->setValue(vBar->value() - delta.y());

hBar = ui->widget2->horizontalScrollBar();
vBar = ui->widget2->verticalScrollBar();

hBar->setValue(hBar->value() + (isRightToLeft() ? delta.x() : -delta.x()));
vBar->setValue(vBar->value() - delta.y());
}

I tried before to make the panning with the translate function and it was not working either (no error but nothing was happening).
Any idea?

Thanks

anda_skoa
4th May 2014, 18:29
Zooming using signal/slot connection is working fine. When I try to do the same thing with panning I am getting the error:

"invalid use of incomplete type class QScrollBar ...hBar->setValue()..."

Have you got the include for QScrollBar?

Cheers,
_

rakefet
5th May 2014, 13:38
Yes. Adding QScrollBar.h made it work. Thank you so much!

nish
6th May 2014, 01:30
I suggest you use a eventFilter() and check if the wheelEvent comes to any of the pix. then propagate the event via signals as you do. This seems more clean and easy to add new functionality later.