使用动画更改 QstackedWidget 的页面

问题描述

我希望能够使用某种动画(如淡入/淡出或其他...)来更改 QStackedWidget 的页面
经过一番研究,我发现 QGraphicsOpacityEffect 可能是可行的,然后我在 here

中找到了此代码

小部件淡入淡出

// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(0);
a->setEndValue(1);
a->setEasingCurve(QEasingCurve::InBack);
a->start(QPropertyAnimation::DeleteWhenStopped);

淡出您的小部件

// w is your widget
QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(this);
w->setGraphicsEffect(eff);
QPropertyAnimation *a = new QPropertyAnimation(eff,"opacity");
a->setDuration(350);
a->setStartValue(1);
a->setEndValue(0);
a->setEasingCurve(QEasingCurve::OutBack);
a->start(QPropertyAnimation::DeleteWhenStopped);
connect(a,SIGNAL(finished()),this,SLOT(hideThisWidget()));
// now implement a slot called hideThisWidget() to do
// things like hide any background dimmer,etc.

但看起来这些代码在 QWidget 内的 QStackedWidget 中使用时有一些问题,我的意思是小部件成功淡入和淡出,但是在动画完成后,如果我最小化窗口,小部件将完全消失! (我仍然可以在我的窗口右下角看到小部件,看起来它的位置改变了?!)
顺便说一句,我的程序是无框架的。
感谢帮助。

这是我的问题的一个例子
test.cpp

Test::Test(QWidget *parent)
    : CustomMainWindow(parent)
{
    ui.setupUi(this);

    setShadow(ui.bg_app);

    connect(ui.close_app_btn,&QPushButton::clicked,&QWidget::close);
    connect(ui.minimize_app_btn,&QWidget::showMinimized);

    QGraphicsOpacityEffect* eff = new QGraphicsOpacityEffect(this);
    ui.checking->setGraphicsEffect(eff); // checking is my widget inside of QStackedWidget.
    QPropertyAnimation* a = new QPropertyAnimation(eff,"opacity");
    a->setDuration(350);
    a->setStartValue(0);
    a->setEndValue(1);
    a->setEasingCurve(QEasingCurve::InBack);
    a->start(QPropertyAnimation::DeleteWhenStopped);
}

CustomMainWindow.cpp

CustomMainWindow::CustomMainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowFlags(windowFlags() | Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
    setAttribute(Qt::WA_TranslucentBackground);
}

void CustomMainWindow::setShadow(QWidget* window)
{
    QGraphicsDropShadowEffect* windowShadow = new QGraphicsDropShadowEffect;
    windowShadow->setBlurRadius(9.0);
    windowShadow->setColor(palette().color(QPalette::Highlight));
    windowShadow->setOffset(0.0);
    window->setGraphicsEffect(windowShadow);
}

当我用这段代码运行我的程序时,起初它成功淡入,但如果我将窗口最小化,小部件从它的原始位置移动到其他地方,看看这个 gif

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...