如何启动淡出动画的QMainWindow ?



我试着这样启动我的窗口:

#include "stdafx.h"
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowOpacity(0);
QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
QPropertyAnimation* ani = new QPropertyAnimation(eff, "windowOpacity");
ani->setDuration(3000);
ani->setStartValue(0);
ani->setEndValue(1);
ani->setEasingCurve(QEasingCurve::OutBack);
ani->start(QPropertyAnimation::DeleteWhenStopped);
}

#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

但是这个窗口从来都不可见,我想把它初始化为渐变效果。

实现它的正确方法是什么?

我看到两个问题。首先,正如最初编写的那样,您的main函数将在打开窗口后立即退出。在main.

末尾添加return a.exec();

。接下来,您将动画QGraphicsOpacityEffect。如前所述,您的示例代码在QGraphicsOpacityEffect和窗口之间没有连接。您的动画在效果类上动画属性,但这不会传播回小部件。不需要使用QGraphicsOpacityEffect。相反,只需直接将小部件动画化:

ui.setupUi(this);
setWindowOpacity(0);
// Notice that the first argument passed in is 'this' - the widget.
// the widget is what you want to animate.
QPropertyAnimation* ani = new QPropertyAnimation(this, "windowOpacity");
ani->setDuration(3000);
ani->setStartValue(0);
ani->setEndValue(1);
ani->setEasingCurve(QEasingCurve::OutBack);
ani->start(QPropertyAnimation::DeleteWhenStopped);

最新更新