galilio
galilio

Reputation: 307

Qt mousePressedEvent called when mouse enter window

I have a MainWindow(::QMainWindow), I rewrote mousePressEvent, mouseReleaseEvent, mouseMoveEvent to track mouse. Everything goes ok except mousePressEvent.

When I press mouse left button and move the mouse, mouseMoveEvent is called, then I move the mouse out of my window and release mouse left button, after this I move the mouse into my window without left button pressed, mousePressedEvent is called. And when I click left button for the first time since mouse entered, mousePressedEvent is not called.

It's weird that mousePressedEvent is called when there is no buttons pressed.

I wonder if this is the default policy of qt or a bug. How can I overcome this ?


After add log to those method, I found that when I move mouse out of window with button pressed, mouseReleaseEvent is called automatically while the button is not released.

Additional I am working on mac os 10.11 with Qt 5.4.0


Finally I found this is not the default policy of Qt. I have this question because I add a NSView to QtWindow.

Upvotes: 0

Views: 761

Answers (1)

Amol Saindane
Amol Saindane

Reputation: 1598

Are you using event(QEvent *curEvent) public slot ? I worked out the way you said, its working fine. As you release Mouse outside MainWindow then you will get MouseRelese event, and when you come back in MainWindow then no mousepress event is called. Please check with below code. I am using QT on Windows with MinGW compiler.

bool MainWindow :: event(QEvent *curEvent)
{
    if(curEvent->type() == QEvent::MouseButtonPress)
    {
        qDebug() << "Mouse Press";
    }

    if(curEvent->type() == QEvent::MouseButtonRelease)
    {
        qDebug() << "Mouse Release";
    }

    if(curEvent->type() == QEvent::MouseMove)
    {
        qDebug() << "Mouse Move";
    }

    return QWidget::event(curEvent);
}

Upvotes: 1

Related Questions