Results 1 to 7 of 7

Thread: How to generate artifitial mouse events?

  1. #1
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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?

  2. #2
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default 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.
    Qt Code:
    1. QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0);
    2. QApplication::sendEvent(mainWindow, &event);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  4. #4
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default 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.

  5. #5
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.
    Qt Code:
    1. bool MyScene::event(QEvent *event)
    2. {
    3. if(event->type() == QEvent::User)
    4. {
    5. qDebug() << "Received!";
    6. click->setButton(Qt::LeftButton);
    7. click->setScenePos(QPointF(((MyEvent *)event)->getX(), ((MyEvent *)event)->getY()));
    8. click->setButtonDownScenePos(Qt::LeftButton, QPointF(((MyEvent *)event)->getX(), ((MyEvent *)event)->getY()));
    9. click->setLastScenePos(QPointF(((MyEvent *)event)->getX(), (((MyEvent *)event)->getY())));
    10. return QGraphicsScene::event(click);
    11. }
    12. else
    13. return QGraphicsScene::event(event);
    14. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default 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.

  7. #7
    Join Date
    Jun 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. void MainWindow::click()
    2. {
    3. QMouseEvent click(QEvent::MouseButtonPress, QPoint(126,575), Qt::LeftButton, 0, 0);
    4. click.setAccepted(true);
    5. QCoreApplication::sendEvent(m_view, &click);
    6. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. m_scene = new MyScene();
    2. m_scene->setSceneRect(0, 0, 600, 600);
    3. m_scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    4. m_scene->setBackgroundBrush(Qt::red);
    5.  
    6. m_view = new QGraphicsView(m_scene);
    7. m_view->setRenderHint(QPainter::Antialiasing);
    8. m_view->setCacheMode(QGraphicsView::CacheBackground);
    9. m_view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    10. m_view->setInteractive(true);
    11. m_view->show();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 05:34
  2. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 15:03
  3. Replies: 9
    Last Post: 22nd June 2008, 22:26
  4. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.