2

我希望能够通过某种动画(如淡入/淡出或其他...)更改 QStackedWidget 的页面
经过一些研究我发现它可能是可能的QGraphicsOpacityEffect,然后我在这里找到了这个代码

淡入你的小部件

// 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, this, &QWidget::close);
    connect(ui.minimize_app_btn, &QPushButton::clicked, this, &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);
}

自定义主窗口.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 在此处输入图像描述

4

0 回答 0