How to generate artifitial mouse events?
I would like to post artifitial mouse events in QGraphicsScene.
I have an application that parses some data and should be able to generate mouse clicks and moves based on it. Sometimes there may be several events at the same time.
I tried createing a new QGraphicsSceneMouseEvent and then set the data fields (setButton(), setPos(), setButtonDownPos(), etc) and then post the event to the Scene, but nothing happens.
Should I set absolutley everything (setPos, setScenePos, setLastPos, setLastScenePos) or setting just the scene positions is enaugh?
Re: How to generate artifitial mouse events?
Hi,
I did this for 'keypress' events using QCoreApplication::sendEvent, you can send any events with this function. There is even a mousepressevent example in the function code.
Re: How to generate artifitial mouse events?
Yes, I have read it, but it is not working for me with QGraphicsScene.
I have a graphics item with reimplemented mousePressEvent, and I am outputting the event coordinates on click, so I know that it works with just clicking with the mouse.
But posting a new QGraphicsSceneMouseEvent (or justQMouseEvent) with the correct buttonDown and position coordinates does nothing.
QGraphicsScene::event() returns false with my manually posted events.
Re: How to generate artifitial mouse events?
Are you posting the event to the correct object? Please provide an example of how you are posting the event using this method.
Re: How to generate artifitial mouse events?
I am posting it to the right object for sure. I derived a class from QGraphicsScene and reimplemented QGraphicsScene::event().
I am posting a custom event to the scene with the coordinates of the desired mouse click and unique id (as I said there may be several clicks at the same time).
In the derived event() implementation I am successfully capturing the custom event and create a new QGraphicsSceneMouseEvent with the coordinates, which I am then passing to the base QGraphicsScene::event() function.
That's where nothing happens.
Code:
bool MyScene
::event(QEvent *event
) {
if(event
->type
() == QEvent::User) {
qDebug() << "Received!";
click->setButton(Qt::LeftButton);
click
->setScenePos
(QPointF(((MyEvent
*)event
)->getX
(),
((MyEvent
*)event
)->getY
()));
click
->setButtonDownScenePos
(Qt
::LeftButton,
QPointF(((MyEvent
*)event
)->getX
(),
((MyEvent
*)event
)->getY
()));
click
->setLastScenePos
(QPointF(((MyEvent
*)event
)->getX
(),
(((MyEvent
*)event
)->getY
())));
}
else
}
Re: How to generate artifitial mouse events?
I assume you have a QGraphicsView in which the scene is using as a view?
I am looking at the QGraphicsSceneMouseEvent and it says.. "When a QGraphicsView receives a QMouseEvent, it translates it to a QGraphicsSceneMouseEvent. The event is then forwarded to the QGraphicsScene associated with the view"
Make sure you are posting the 'artificial' event to your view (QWidget/QGraphicsView), your 'view' will then forward that 'artificial' event to your scene.
see QMouseEvent. Where is this "event->type() == QEvent::User" this user event being posted from? FYI.. be careful of that 'new' operator in the event handler, make sure you are freeing your memory somewhere logical.
Re: How to generate artifitial mouse events?
I am creating the events in the MainWindow.
I have already tried to send the event to the view, but nothing happens again?!
I even set it to setInteractive(true) although it should be enabled by default.
The code is:
Code:
void MainWindow::click()
{
click.setAccepted(true);
}
I am checking incoming events in the scene derived class and there are no QGraphicsSceneMouseEvents, unless I click with the mouse.
These are the scene and view initializations:
Code:
m_scene = new MyScene();
m_scene->setSceneRect(0, 0, 600, 600);
m_scene->setBackgroundBrush(Qt::red);
m_view
->setRenderHint
(QPainter::Antialiasing);
m_view
->setViewportUpdateMode
(QGraphicsView::BoundingRectViewportUpdate);
m_view->setInteractive(true);
m_view->show();