Important: Please read the Qt Code of Conduct -

Customize mouseReleaseEvent

  • This mouseReleaseEvent is working fine,

    @void mouseReleaseEvent ( QMouseEvent * event );@

    @void Lines::mouseReleaseEvent ( QMouseEvent * event ) {

    if ( event->button() == Qt::LeftButton)

    msgBox = new QMessageBox();
    msgBox->setText("You Clicked Left Mouse Button");

    But I need this to work only on a specific widget.

    so something like

    @ if ( event->button() == Qt::LeftButton) && ( widget == widget_1 )
    { do something}@

    @ if ( event->button() == Qt::LeftButton) && ( widget == widget_2 )
    { do something_else}@

    Can anyone help me please,
    I have been searching like for ever.

    I know I can do it via checking the x-y pos
    @if (( event->button() == Qt::LeftButton) && ((event->pos().x() <=20) ))@

    But this checks the total position,
    so if I move widget the coordinates are not correct anymore.

  • event->pos() == widget->pos() ?

    Or you can reimplement the mouseReleaseEvent for the widget you are trying to get the event from. It should overwrite the event from the parent in theory. May be speaking nonsense as I am not entirely sure. I know that for keyPressEvents this is happening.

  • This works in the class where you implement it. You can add a flag to the class that is checked whether the handling should be done or not.
    Or you use event filters for that.

  • thanks for your answers,
    but you could provide little bit of code, pelase.

    I have been trying but nothing really works.

  • which code should I provide?
    I have no need for this feature :-)
    I give you hints, you implement it, that's the way it works...

    So, if you have a custom widget class, e.g. MyLineEdit derived from QLineEdit, you overwrite mouseReleaseEvent as in your first post.
    Your class gets a poroperty: handleMouseRelease and depending on the value of that property, you handle it or not.

    Other option: The container creates an eventFilkter on clients (look at QObject and search eventFilter) and you handle it there.

Log in to reply