Bharathi
Bharathi

Reputation: 477

Long Mouse Press on QToolButton calls the released signal

I have a QToolButton defined. I have written two slots for pressed and released signals.

void EyImgGalleryWindow::toolPressed()
{
 if(Uptimer->timerId() == -1)
    Uptimer->start();
 m_CustomGalleryView->scrollUp();
}

void EyImgGalleryWindow::toolReleased()
 {
   Uptimer->stop();
   m_CustomGalleryView->scrollUpRelease();
 }

When the pressed() signal is emitted, I am starting a timer and when the released signal is emitted I am stopping the timer.

So here my implementation is when the timer exceeds 3s I am updating a variable. I have to update the variable only when the User have a long press on this QToolButton.

But here my implementation is failing in this case. When I have a long press on QToolButton, It is emitting the Pressed and Released signal on equal intervals. When a long press is there we should get only once the Released signal and Why here it is calling the Released signal multiple times. Is there anything i am doing wrong here?

Upvotes: 0

Views: 889

Answers (1)

Jablonski
Jablonski

Reputation: 18504

You can catch long press with next event filter and QElapsedTimer:

QElapsedTimer el;//somewhere

//...

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{

    if(obj == ui->pushButton_13 && event->type() == QEvent::MouseButtonPress)
    {
        el.start();
    }
    if(obj == ui->pushButton_13 && event->type() == QEvent::MouseButtonRelease)
    {
        qDebug() << el.elapsed();//in milliseconds, you can divide by 1000 to get seconds
        if(el.elapsed() > 1000)
            qDebug() << "long";
        else
            qDebug() << "not long";
    }
    return QObject::eventFilter(obj, event);
 }

It will work for pushButtons, labels, toolButtons etc.

Upvotes: 3

Related Questions