PDA

View Full Version : Create a QWheelEvent and pass it to a widget's wheelEvent



almagest
20th December 2010, 14:21
Hello.

I am creating a grid (like a graph) from a QGraphicsView adding zooming capabilities.
If i use the scrollwheel on the graphicsview, the application zoomes the view as intended.
I now want to map a button to emit a QWheelEvent signal to the graphicsview widget.
(a zoom button)

the slot function for the buttons clicked() signal.


// slot function for the clicked() signal of the zoombutton
void DSATS::plusZoom()
{
std::cout << "should zoom" << std::endl;

QWheelEvent * event = new QWheelEvent(QPoint(10, 10), 120, Qt::LeftButton, Qt::NoModifier);
QApplication::postEvent(dsatsGrid, event);

}

Here dsatsGrid is a subclassed QGraphicsView.




// wheelevent handler in the grid-class
void GfxGrid::wheelEvent(QWheelEvent* event)
{

std::cout << "wheelevent recieved" << std::endl;

...

}

When i click the button, the function zoomPlus is called as expected, but the wheelevent never reaches its intended target.
I have tried with both postEvent and sendEvent, but it makes no difference. (although i know postEvent should be used because its thread safe and handles the memory)

Thank you for any help!

Lykurg
20th December 2010, 14:26
Creating a event to post it seems to be a little bit too much in my eyes. Better create a public function for that and call it direct:

void GfxGrid::zoom(qreal factor) //...

void GfxGrid::wheelEvent(/*...*/)
{
// calculate factor
zoom(factor);
}

void DSATS::plusZoom()
{
dsatsGrid->zoom(1.1);
}

almagest
20th December 2010, 14:42
Hello, and thanks for the quick reply.

I realize the problem can be solved as easy as you describe, but am curious to know why it cannot/does not work the way i tried it. As i understand it, the postEvent()-function and the corresponding event type is meant to accomplish exactly what i am trying to achieve.

Thanks for your time.

Lykurg
20th December 2010, 14:58
It should work, but probably the event gets eaten somewhere else. How do you have defined the pointer dsatsGrid? Can you make a small executable project reproducing your problem.

EDIT: Here I made a simple working pice of code:
#include <QtGui>

class Test : public QWidget
{
Q_OBJECT
public:
Test(QWidget *parent) : QWidget(parent)
{}

protected:
void wheelEvent(QWheelEvent *event)
{
qWarning() << Q_FUNC_INFO << event;
}
};


class Base : public QPushButton
{
Q_OBJECT

public:
Base(QWidget *parent) : QPushButton(parent)
{
setText("send weel event");
test = new Test(this);
connect(this, SIGNAL(clicked()),this, SLOT(send()));
}

private Q_SLOTS:
void send()
{
QWheelEvent event(QPoint(10, 10), 120, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(test, &event);
}

private:
Test *test;
};




int main(int argc, char *argv[])
{
QApplication app(argc, argv);

Base b(0);
b.show();

return app.exec();
}

#include "main.moc"