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);

  • Qt Champions 2016

    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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.