Results 1 to 3 of 3

Thread: Artificial mouse events

  1. #1
    Join Date
    Aug 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Artificial mouse events

    Small toy app can be found here: http://gist.github.com/517445

    I am trying to send artificial mouse event to widget and I use QApplication::sendEvent for that, next I check ev.isAccepted() and it returns False, even worse! Widget I've sent event to doesnt handle it (it is calendar widged and no date is picked) and I am in doubt that it even receives it, because I can see how mouseEventPressed is fired up on parent widget.

    Qt Code:
    1. #include "calendar.h"
    2.  
    3. Calendar::Calendar(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. qCal = new QCalendarWidget;
    7. qBtn = new QPushButton(tr("Press me"));
    8.  
    9. connect(qBtn, SIGNAL(clicked()), this, SLOT(testCustomClick()));
    10.  
    11. QVBoxLayout *layout = new QVBoxLayout;
    12. layout->addWidget(qCal);
    13. layout->addWidget(qBtn);
    14.  
    15. setLayout(layout);
    16. qDebug() << "Date:" << QDate::currentDate();
    17. }
    18.  
    19. Calendar::~Calendar()
    20. {
    21. }
    22.  
    23. void Calendar::testCustomClick()
    24. {
    25. QMouseEvent qm2(QEvent::MouseButtonPress, QPoint(qCal->width()/2,qCal->height()/2), Qt::LeftButton , Qt::LeftButton, Qt::NoModifier);
    26. QApplication::sendEvent(qCal, &qm2);
    27.  
    28. //this one is always False
    29. qDebug() << "isAccepted: " << qm2.isAccepted();
    30. }
    31.  
    32.  
    33. void Calendar::mousePressEvent(QMouseEvent* ev)
    34. {
    35. //this is executed even for QMouseEvent which sended to qCal =((
    36. //if we click on qCal with real mouse it is not executed
    37. qDebug() << "mouse event: " << ev << "x=" << ev->x() <<" y=" << ev->y();
    38. QWidget::mousePressEvent(ev);
    39. }
    To copy to clipboard, switch view to plain text mode 

    I've done some search over forum and stackoverflow and found similair questions all of them are without solution =(

  2. #2
    Join Date
    Aug 2010
    Posts
    6
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Artificial mouse events

    The reason for this not working is simple: To simulate a mouse click you have to send both a MouseButtonPress-Event AND a MouseButtonRelease-Event.
    So try this:
    Qt Code:
    1. void Calendar::testCustomClick()
    2. {
    3. {
    4. QMouseEvent qm2(QEvent::MouseButtonPress, QPoint(qCal->width()/2,qCal->height()/2), Qt::LeftButton , Qt::LeftButton, Qt::NoModifier);
    5. QApplication::sendEvent(qCal, &qm2);
    6. }
    7. {
    8. QMouseEvent qm2(QEvent::MouseButtonRelease, QPoint(qCal->width()/2,qCal->height()/2), Qt::LeftButton , Qt::LeftButton, Qt::NoModifier);
    9. QApplication::sendEvent(qCal, &qm2);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    Tobi

  3. #3
    Join Date
    Aug 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Artificial mouse events

    I've tried it as well, it doesnt work. I've digged into Qt source code and found
    that sendEvent calls notify function which itself calls widget->event, but QCalendarWidget::event doesn't handle mouse events instead it call QTableView::event which itself ends up in QAbstractScrollArea::event which returns false on every mouse event.

    What to do in such situation? How does real hardware mouse sends events to QCalendarWidget? Maybe it sends it to exact widget under cursor (not QCalendarWidget itself but smaller one which QCalendarWidget consist of?)

Similar Threads

  1. QwtDial - Artificial Horizon
    By Kerubu in forum Qwt
    Replies: 2
    Last Post: 19th July 2010, 10:16
  2. Mouse Events
    By daviddoria in forum Qt Programming
    Replies: 6
    Last Post: 13th May 2008, 12:55
  3. mouse events
    By xyzt in forum Newbie
    Replies: 3
    Last Post: 23rd March 2008, 12:14
  4. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 07:13
  5. Mouse Events in a widget
    By Rayven in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2006, 15:07

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.