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

Animate window (top level widget) opacity



  • I am trying to fade in a window. When the program starts, the window is shown as black and animates opacity as required. But I would like window to be shown completely transparent at the start and removing transparency to be shown completely. Here is the code I use:

    QPropertyAnimation* a1 = NULL;
    if (w) { // I checked, it is not null.
        QGraphicsOpacityEffect *eff = (QGraphicsOpacityEffect *)w->graphicsEffect();
        if (!eff) {
            eff = new QGraphicsOpacityEffect(w);
            from->setGraphicsEffect(eff);
        }
        if (dynamic_cast<QMainWindow*>(w))
            a1 = new QPropertyAnimation(eff, "windowOpacity"); // As top level window, uses this one.
        else
            a1 = new QPropertyAnimation(eff, "opacity");
        a1->setDuration(duration);
        a1->setStartValue(0.0);
        a1->setEndValue(1.0);
        a1->setEasingCurve(QEasingCurve::Linear);
        a1->start(QPropertyAnimation::DeleteWhenStopped);
    }
    

    BTW I tried both opacity and windowOpacity and effect is the same.
    Window creation uses this code:

    setWindowFlags(windowFlags() | Qt::WindowMinMaxButtonsHint | Qt::CustomizeWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

  • Lifetime Qt Champion

    hi , i tried your code (win 7, qt 5.5)

    MainWindow::MainWindow(QWidget* parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
    ui->setupUi(this);
    setWindowOpacity(0.0);
    QPropertyAnimation *a1 = new QPropertyAnimation(this, "windowOpacity"); // As top level window, uses this one.
    a1->setDuration(3000);
    a1->setStartValue(0.0);
    a1->setEndValue(1.0);
    a1->setEasingCurve(QEasingCurve::Linear);
    a1->start(QPropertyAnimation::DeleteWhenStopped);
    }

    and it fade in very smooth.

    So what platform are you on?



  • @mrjj - I tried the same code in Mac platform using Qt 5.12.6. But still it is not working.


  • Lifetime Qt Champion

    @shanmugaraja
    Hi
    This poster sounds like it does work on macOS
    https://forum.qt.io/topic/59453/solved-widget-fade-in-effect-possible/3

    so if not working on macOS, i guess its not supported if code is 100% the same.


Log in to reply