User 23
User 23

Reputation: 23

Qt mousePressEvent left button

I am using Qt but have run into a problem.

I want to register when the mouse is pressed and released on a QTableWidget. There are signals only for pressed, not released. Therefore I use mousePressEvent and mouseReleaseEvent instead.

MyWidget.h

protected:
    void mousePressEvent(QMouseEvent * event);
    void mouseReleaseEvent(QMouseEvent * event);

MyWidget.cpp

void MyWidget::mousePressEvent(QMouseEvent *event)
{
    qDebug() << "Pressed";
}

void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
    qDebug() << "Released";
}

It prints "Released" when the left, right or middle mouse button is released over the table. However, it prints "Pressed" only when the right or middle button is pressed.

What could be wrong? How can I register when the left button is pressed?

Upvotes: 1

Views: 9061

Answers (3)

User 23
User 23

Reputation: 23

I found the solution (with help from the other answers):

There was a QFrame on top of the widget that consumed the event. I didn't think of that since it worked for the other buttons. It was solved by:

ui->frame->setAttribute(Qt::WA_TransparentForMouseEvents, true);

Upvotes: 1

skypjack
skypjack

Reputation: 50568

In the documentation of QMouseEvent, it is said that:

A mouse event contains a special accept flag that indicates whether the receiver wants the event. You should call ignore() if the mouse event is not handled by your widget. A mouse event is propagated up the parent widget chain until a widget accepts it with accept(), or an event filter consumes it.

Because of that, we can suppose that probably the events you mentioned are consumed by some other actors.

It would help knowing who are those actors.

Upvotes: 2

Ilya
Ilya

Reputation: 5557

The cell widget probably consumes the mousePressEvent on the left mouse button, so you (the parent widget) are not receiving it.

Upvotes: 1

Related Questions