How can you still use the functionalities of a widget if set to Transparent for Mouse Events? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can you still use the functionalities of a widget if set to Transparent for Mouse Events? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a widget on top of my mainwindow, more like and Advertisement Banner. It is preventing the user from using some functionalities behind it so I set it to

setAttribute( Qt::WA_TransparentForMouseEvents );

But that specific ad banner has 2 buttons, and if it set as ‘transparent’, they can’t be used obvoiusly. My question is that, how can I still use the button functionalities inside the banner of it set to transparent for mouse events?

It looks like this:

enter image description here

Answer

Made a solution using the Mouse Events of the said Banner Widget:

void advertisement::mouseMoveEvent(QMouseEvent *event)
{
    qDebug()<< event->button();
    if (isRightBtn)
    {
        this->setAttribute(Qt::WA_TransparentForMouseEvents);
        main->_IsCamRotate = true;
        main->triggerMouseMove(event);
    }
}

void advertisement::mousePressEvent(QMouseEvent *event)
{
    qDebug()<< event->button();
    switch(event->button())
    {
    case Qt::MouseButton::RightButton:
        isRightBtn = true;
        break;
    }
}


void advertisement::mouseReleaseEvent(QMouseEvent *event)
{
    if (!this->rect().contains(event->pos()) && event->button() == Qt::MouseButton::RightButton)
    {
        main->triggerMouseRelease(event);
        isRightBtn = false;

        this->setAttribute(Qt::WA_TransparentForMouseEvents, false);
    }
}
We are here to answer your question about How can you still use the functionalities of a widget if set to Transparent for Mouse Events? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji