Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Qt, what QEvent means loses window focus, regain window focus? (Set transparency)

Tags:

qt

I need to set transparency when my application loses focus. I also need to reset the transparency when it regains focus (from a mouse click or alt-tab or whatever)

I know how to set the transparency, so that is not the issue: setWindowOpacity(0.75);

The issue is WHEN?

like image 537
relipse Avatar asked Jan 17 '13 10:01

relipse


3 Answers

I agree with Kévin Renella that there are sometimes issues with QWidget::focusInEvent and QWidget::focusOutEvent. Instead, a better approach would be to implement QWidget::changeEvent():

void MyQWidget::changeEvent(QEvent *event)
{   
    QWidget::changeEvent(event);
    if (event->type() == QEvent::ActivationChange)
    {
        if(this->isActiveWindow())
        {
            // widget is now active
        }
        else
        {
            // widget is now inactive
        }
    }
}

You can also achieve the same thing by installing an event-filter. See The Event System on Qt Documentation for more information.

like image 77
Sachin Joseph Avatar answered Oct 21 '22 18:10

Sachin Joseph


When a QFocusEvent event occurs. Just re-implement

void QWidget::focusInEvent ( QFocusEvent * event );
void QWidget::focusOutEvent ( QFocusEvent * event );

from QWidget. Make sure to always call the super-class method before or after doing your work. i.e., (before case)

void Mywidget::focusInEvent (QFocusEvent * event ){
   QWidget::focusInEvent(event);
   // your code
}

But, there are sometimes issues with QWidget::focusInEvent and QWidget::focusOutEvent. See this answer for a more reliable approach.

like image 24
UmNyobe Avatar answered Oct 21 '22 17:10

UmNyobe


There are sometimes issues with QWidget::focusInEvent and QWidget::focusOutEvent events of QWidget

There is an alternative using QWidget::windowActivationChange(bool state). True, your widget is active, false otherwise.

like image 22
Kévin Renella Avatar answered Oct 21 '22 16:10

Kévin Renella