Important: Please read the Qt Code of Conduct -

MousePressEvent doesn't get called when left mouse button clicked

  • However it does get called when right or middle mouse button is clicked. Does any one know what's wrong?

    @void MyWindow::mousePressEvent(QMouseEvent *event)
    if(event->button() == Qt::LeftButton)
    std::cout << "left mouse click " << std::endl; // doesn't work
    else if (event->button() == Qt::RightButton)
    std::cout << "right mouse click " << std::endl; // does work
    else if (event->button() == Qt::MidButton)
    std::cout << "middle mouse click " << std::endl; /// does work

  • try a qDebug() << event->button(); and check the value... it should be working because the reimplementation seems ok.

  • I inserted qDebug statement at head of function. The values 2 and 4 were output upon right and middle mouse button clicks but all was silent for left mouse button click. Can somehow left mouse button event be disabled or accepted already by a parent widget ? Except that MyWindow is a QMainWindow
    ??? hmmmm
    Any suggestions appreciated

  • Ok this is odd. Sorry but I have no idea as to why this is happening...

  • Reading the docs, you may notice that there is a buttons() (notice the s at the end) as well, which will be more useful for you.
    if ((event->buttons() & Qt::LeftButton) == Qt::LeftButton)
    qDebug() << "Left mouse clicked";

Log in to reply