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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With