Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Click with the mouse through a label



  • hello to the whole community Qt Forum,
    I would like to know if there is an option for qLabel that allows me to make it transparent only for the mouse, but in reality it must be seen; That is, this label contains a text, but I need to click below.
    I hope I was clear
    Thanks in advance :)


  • Moderators

    Yes, you just need to reimplement the mouse click event and set accepted property to false.

    Example for widgets:

    void MyLabel::mousePressEvent(QMouseEvent *event) {
      QLabel::mousePressEvent(event);
      event.setAccepted(false);
    }
    
    void MyLabel::mouseReleaseEvent(QMouseEvent *event) {
      QLabel::mouseReleaseEvent(event);
      event.setAccepted(false);
    }
    

    QML example:

    Label {
      MouseArea {
        onClicked: mouse.accepted = false
      }
    }
    

    Brain-to-terminal, untested. But might work :-)


  • Lifetime Qt Champion

    Hi,

    IIRC, you can set the Qt::WA_TransparentForMouseEvents widget attribute and it should behave the way you'd like.


Log in to reply