handle mouse events for touchscreen use



  • Good day, all!
    I have touchscreen on device I program for.
    And there are some issues, when I press the screen with fingers. Sometimes it generates few wrong clicks.
    So I try to handle these first clicks with global eventFilter. First few clicks it forbids to process mousePress event. And after some time, it allows to do it.

    I have some problem there.
    So, lets say, user click on point outside the button and later on move finger in, I post mousePress event and mouseRelease event straight away. mousePress achive button, but mouseRelease doesn't! So button is always on after that. What would stop button to receive mouseRelease? I thought, that some enter events, but it's not.
    Advice me please, if you can, what could cause it.

    There is some code:

    function to send press event back to application

    void MainWindow::sendCLick(int x, int y){
        QPoint qp = QPoint(x,y);
        QWidget *QW = qApp->widgetAt(qp);
        if (QW){
            clickSent=1;
            QPoint qp1=QW->mapFromGlobal(qp);
            QMouseEvent* evt1 = new QMouseEvent(QEvent::MouseButtonPress,
                                            qp1,
                                            qp,
                                            Qt::LeftButton,
                                            Qt::LeftButton,
                                            Qt::NoModifier);
            QCoreApplication::postEvent(QW,evt1);
        }
    }
    

    function to send release event

    void MainWindow::sendRelease(int x, int y){
        QPoint qp = QPoint(x,y);
        QWidget *QW = qApp->widgetAt(qp);
        if (QW){
            QPoint qp1=QW->mapFromGlobal(qp);
            QMouseEvent* evt1 = new QMouseEvent(QEvent::MouseButtonRelease,
                                            qp1,
                                            qp,
                                            Qt::LeftButton,
                                            Qt::LeftButton,
                                            Qt::NoModifier);
       }
    }
    
    

    There is my global event filter

    bool MainWindow::eventFilter(QObject *obj, QEvent *evt){
        if (evt->type()==QEvent::MouseButtonPress){
            if (clickSent){
                clickSent=0;
                QPoint qp=QCursor::pos();
                sendRelease(qp.rx(),qp.ry());
                return QMainWindow::eventFilter(obj,evt);
            }
    
            if (!clickedOn){
                timeAfterClick=0;
                QPoint pos=QCursor::pos();
                pseudoMouseX=pos.rx();
                pseudoMouseY=pos.ry();
                clickedOn=1;
            }
            return true;
    
        }
    
        if (evt->type()==QEvent::MouseButtonRelease){
            if ((!QApplication::mouseButtons())&&clickedOn){
                clickedOn=0;
            }
        }
        return QMainWindow::eventFilter(obj,evt);
    }
    

    And after some time I sent these events by timer

    pseudoMouseX=CCur.rx();
    pseudoMouseY=CCur.ry();
    timeAfterClick++;
    if (timeAfterClick>10){
        timeAfterClick=1;
        clickSent=1;
        sendCLick(pseudoMouseX,pseudoMouseY);
        sendRelease(pseudoMouseX,pseudoMouseY);
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Something's not clear here, why do you need to generate your own mouse event ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.