Results 1 to 1 of 1

Thread: How to ignore Button-Events in Qtopia?

  1. #1
    Join Date
    Jul 2007
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to ignore Button-Events in Qtopia?

    Hello folks,

    I have a problem with events in Qtopia. If events occur, while our one-and-only main thread is doing some other stuff besides events handling, they are dispatched deferred. If events occur in the meantime, they might be delivered to buttons, which were not active, when the event actually happened. Therefor we wanted to be able to discard all pending button events, if desired.

    The following simple example shows the problem: After pressing "start", that button should be disabled for 2 seconds and then, all intermediate button events should be discarded. While our eventFiltering-solution works on Windows, it fails on Qtopia. We tried different workarounds, but haven't yet found a solution for the problem. If you hit the disabled button in our example, the event is deferred until the button is enabled again and then dispatched.

    It seems as if there is an event queue somewhere else, that is unreachable for us. Is there any idea, what went wrong?

    Thanks for your help.
    Helmut

    ==================

    The example consists of:
    MyWidget.cpp
    MyWidget.h
    PressDisabled.pro

    ==================
    === MyWidget.cpp ===
    ==================
    Qt Code:
    1. /*
    2.  * The example shows a widget including a start button to start a longer job
    3.  * (a 2 seconds wait in the example). In our application, that job is done in
    4.  * the main thread without further event processing. The start button should
    5.  * therefore be visually deactivated in the meantime. After the job
    6.  * the start button gets enabled again to start another job.
    7.  *
    8.  * The problem is, that the user can hit the deactivated button during
    9.  * the job processing time. These hits should be ignored, when the button
    10.  * gets enabled again.
    11.  *
    12.  * On windows, we solved the problem with an event filter, which filters
    13.  * out the invalid presses after the job has completed.
    14.  *
    15.  * This mechanism seems not to work with Qtopia. We tried hard but were not
    16.  * able to filter out the invalid events, which lead to an automatic restart
    17.  * of the job. It seems as if the mouse events are stored somewhere outside
    18.  * the event queue and not reachable at the time when we want to discard them.
    19.  *
    20.  * Is there any idea for a workaround in Qtopia?
    21.  */
    22. #include <QApplication>
    23. #include <QWidget>
    24. #include <QTime>
    25.  
    26. #include "MyWidget.h"
    27.  
    28. MyWidget::MyWidget(QWidget *parent)
    29. : QWidget(parent)
    30. {
    31. setFixedSize(75, 90);
    32.  
    33. count=new QLabel("0",this);
    34. count->setGeometry(0, 0, 75, 30);
    35.  
    36. start = new QPushButton(tr("Start"), this);
    37. start->setGeometry(0, 30, 75, 30);
    38. connect(start, SIGNAL(clicked()), this, SLOT(startClicked()));
    39.  
    40. QPushButton *quit = new QPushButton(tr("Quit"), this);
    41. quit->setGeometry(0, 60, 75, 30);
    42. connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    43. }
    44.  
    45. // a filter to be used for dicarding mouse events on the start button
    46. bool MyWidget::eventFilter(QObject *obj, QEvent *event)
    47. {
    48. if ( obj==start &&
    49. (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick)) {
    50. qDebug("BINGO: Button event filtered out.");
    51. return true;
    52. }
    53. else {
    54. return false;
    55. }
    56. }
    57.  
    58. // just a try to remove all events for the start button (but not working)
    59. void MyWidget::dropButtonEvents() {
    60. QCoreApplication::removePostedEvents(start);
    61. }
    62.  
    63. // ==============================================
    64. // the slot of the start button, that does the whole stuff
    65. // ==============================================
    66. void MyWidget::startClicked() {
    67. qDebug("startClicked() entered");
    68. count->setNum(count->text().toInt()+1);
    69. // disable start button and show that on the screen
    70. start->setEnabled(false);
    71. QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
    72. start->installEventFilter(this);
    73.  
    74. qDebug("Processing started");
    75. QTime time;
    76. time.start();
    77. // loop for a while to simulate some hard work ...
    78. while(time.elapsed()<2000);
    79. qDebug("Processing stopped");
    80.  
    81. // TODO: find a solution to clean up with Button Events
    82. // the solution should also work with Qtopia!
    83. dropButtonEvents();
    84.  
    85. // enable start button again
    86. start->setEnabled(true);
    87. // AllEvents used in processEvents, so we can filter buttons (at least on Windows)
    88. QCoreApplication::processEvents(QEventLoop::AllEvents);
    89. start->removeEventFilter(this);
    90.  
    91. qDebug("startClicked() left");
    92. }
    93.  
    94. int main(int argc, char *argv[])
    95. {
    96. // the basic application start with a single widget
    97. QApplication app(argc, argv);
    98. MyWidget widget;
    99. widget.show();
    100. return app.exec();
    101. }
    To copy to clipboard, switch view to plain text mode 
    ==================
    === MyWidget.h ===
    ==================
    Qt Code:
    1. #include <QPushButton>
    2. #include <QLabel>
    3. #include <QEvent>
    4.  
    5. class MyWidget : public QWidget
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. MyWidget(QWidget *parent = 0);
    11.  
    12. QLabel *count;
    13. QPushButton *start;
    14.  
    15. bool eventFilter(QObject *obj, QEvent *event);
    16.  
    17. void dropButtonEvents();
    18.  
    19. public slots:
    20. void startClicked();
    21.  
    22. };
    To copy to clipboard, switch view to plain text mode 
    ==================
    === PressDisabled.pro ===
    ==================

    CONFIG += qt debug

    SOURCES = MyWidget.cpp \

    HEADERS = MyWidget.h \

    FORMS =
    Last edited by jacek; 5th July 2007 at 23:16. Reason: missing [code] tags

Similar Threads

  1. Replies: 3
    Last Post: 6th March 2007, 19:24
  2. QPushButton:: Handle right mouse button events. Easyway?
    By Harvey West in forum Qt Programming
    Replies: 6
    Last Post: 28th February 2007, 17:56
  3. Ignore mouse events out of tree view
    By krishna.bv in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2006, 12:24

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.