Touch anywhere to launch an application



  • Hello all,

    I need to develop a QT application for a embedded device where if i tap anywhere on the screen 5 times(just an example) within a specific time interval, I should be able to launch another application.

    What is the best way to get the touch event notification in my application ? The criteria to launch the application could also be a long press event ? Does QT support capturing long press events ?

    I am using QT embedded version 4.6.0 on a ARM based target board.



  • Yes, Qt can do both of them.
    There is one way, but not the best way, I think.

    • Specific times press:

    Reimplement mousePressEvent and mouseReleaseEvent.

    Use a self increasing counter to count times of pressed in mouseReleaseEvent. Check if it reached the specific times. If reached, do what you want to do there.

    Use a timer to garantee time interval. Start the timer in mousePressEvent when first tap, Stop the timer and clear the counter when time out.

    some code snap:
    @
    #include "widget.h"
    #include "ui_widget.h"

    Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    isPressed(false),
    timesOfPressed(0),
    isFistPressed(true),
    isTimerTimeout(false)
    {
    ui->setupUi(this);

    timer = new QTimer(this);
    timer->setInterval(2000);
    connect(timer, SIGNAL(timeout()),this, SLOT(pressTimeOut()));
    

    }

    Widget::~Widget()
    {
    delete ui;
    }

    void Widget::mousePressEvent(QMouseEvent *event)
    {
    if(event->button() == Qt::LeftButton) {
    isPressed = true;
    if (isFistPressed) {
    isFistPressed = false;
    timer->start();
    isTimerTimeout = false;
    }
    }
    }

    void Widget::mouseReleaseEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton && isPressed) {
    timesOfPressed++;
    isPressed = false;
    checkPressed();
    }
    }

    void Widget::checkPressed()
    {
    qDebug() << "You have pressed: " << timesOfPressed << "times";

    if (timesOfPressed == 5) {
        timesOfPressed = 0;
        timer->stop();
        isFistPressed = true;
    
        qDebug() << "Finally get here, do something...";
        // call another application with QProcess.
    }
    
    if (isTimerTimeout) {
        timesOfPressed = 0;
        qDebug() << "This round already time out, clear counter ";
    }
    

    }

    void Widget::pressTimeOut()
    {
    qDebug() << "This round time out,recounting.";
    timer->stop();
    isTimerTimeout = true;
    timesOfPressed = 0;
    isFistPressed = true;
    }
    @

    • Long time press:

    Start a timer in mousePressEvent.

    Stop the timer in mouseReleaseEvent.

    Once the timer time out, stop the timer and do what you want to do.

    some code snap:
    @
    ...
    longPressTimer = new QTimer(this);
    longPressTimer->setInterval(1000);
    connect(longPressTimer, SIGNAL(timeout()), this, SLOT(doSomething()));
    ...

    void QWidget::mousePressEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton) {
    longPressTimer->start();
    }
    }

    void QWidget::mouseReleaseEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton) {
    longPressTimer->stop();
    }
    }

    void QWidget::doSomething()
    {
    longPressTimer->stop();
    // something...
    }
    @



  • Do you want to do it with QML or with QT ?


Log in to reply
 

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