Search code examples
qtqt-events

QTouchEvent always called 4 times with any interaction


QTouchEvent always called 4 times with any interaction with the screen. Even if i just touch the screen for a 0.1 second (not moving and not released the finger). So i can`t get an information when the touch is actually was ends or updated.

bool ChartPlotter::touchEvent(QTouchEvent* ev) {

    switch (ev->type()) {
       case QTouchEvent::TouchBegin: {
           __android_log_write(ANDROID_LOG_WARN,"","begin");
       }
       case QTouchEvent::TouchUpdate: {
        __android_log_write(ANDROID_LOG_WARN,"","update");
    }
       case QTouchEvent::TouchEnd: {
             __android_log_write(ANDROID_LOG_WARN,"","end");
    }
 ...

and the output when i just touched the screen (i not moved the finger, and not released it, i just touched and holded it on on the same place)

W   : begin
W   : update
W   : end
W   : end
``

Solution

  • You are missing a break for each case, because like this it will go trough all cases.

    You don't need a {} for the case, it's fine like this:

    bool ChartPlotter::touchEvent(QTouchEvent* ev) {
    
    switch (ev->type()) {
       case QTouchEvent::TouchBegin:
           __android_log_write(ANDROID_LOG_WARN,"","begin");
       break;
    
       case QTouchEvent::TouchUpdate: 
           __android_log_write(ANDROID_LOG_WARN,"","update");
       break;
    
       case QTouchEvent::TouchEnd:
           __android_log_write(ANDROID_LOG_WARN,"","end");
       break;
    }
    ...
    

    Since you make a TouchBegin event, it prints out all 3 logs (since there is no break to stop them), and once you release the press, TouchEnd is triggered and because of that you have end printed out two times.