qt:如何使用 QPropertyAnimation 为子 QPushButton 的透明度设置动画?

标签 qt animation

我想在 2 秒的时间内逐渐降低 QPushButton 的不透明度以完全透明。为此,我使用了 QPropertyAnimation 类并使用按钮的属性“windowOpacity”来实现效果。但这仅适用于独立的 QPushButton。当我为按钮分配父级时,效果消失了。有没有办法让子按钮达到同样的效果?

最佳答案

windowOpacity 属性仅适用于顶级窗口,因此不幸的是,它无法帮助您在子窗口小部件上设置透明度动画。

标准控件有点问题,并且有许多因素影响其最终外观。您可以采取多种方法,但它们都涉及一定量的编码。没有简单的方法:)

要设置QPushButton的透明度,您需要为其设置样式表,或者更改调色板的某些属性。由于QPropertyAnimation无法直接使用这些选项,因此您可以创建自己的自定义属性并为其设置动画。

下面是一些代码,为 MainWindow 指定一个名为 alpha 的自定义属性。 Alpha 值用于设置按钮颜色的 Alpha 部分。有了这个属性,我们就可以使用 QPropertyAnimation 来为其设置动画。结果是一个淡入和淡出的按钮。这仅处理按钮背景而不是文本,但它应该为您提供一个起点。

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QPushButton>

class MainWindow : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int alpha READ alpha WRITE setAlpha);

public:
    MainWindow();
    virtual ~MainWindow();

private:
    int m_alpha;
    QPushButton * m_button1, *m_button2;

    int alpha() const;
    void setAlpha(const int a_alpha);
};

#endif  /* MAINWINDOW_H */

MainWindow.cpp: (已更新以包含样式表透明度示例)

#include <QPlastiqueStyle>
#include <QPropertyAnimation>

#include "MainWindow.h"

MainWindow::MainWindow() :
    m_button1(0),
    m_button2(0),
    m_alpha(255)
{
    resize(200, 200);
    QPalette windowPalette(palette());
    windowPalette.setBrush(QPalette::Background, QBrush(QColor(200, 0, 0)));
    setPalette(windowPalette);

    m_button1 = new QPushButton(this);
    m_button1->setText("Palette Transparency");
    m_button1->setAutoFillBackground(false);
    // NOTE: Changing the button background color does not work with XP Styles
    // so we need to use a style that allows it.
    m_button1->setStyle(new QPlastiqueStyle());

    m_button2 = new QPushButton(this);
    m_button2->move(0, 50);
    m_button2->setText("Stylesheet Transparency");
    m_button2->setAutoFillBackground(false);
    m_button2->setStyle(new QPlastiqueStyle());

    QPropertyAnimation *animation = new QPropertyAnimation(this, "alpha");
    animation->setDuration(1000);
    animation->setKeyValueAt(0, 255);
    animation->setKeyValueAt(0.5, 100);
    animation->setKeyValueAt(1, 255);
    animation->setLoopCount(-1);
    animation->start();
}

MainWindow::~MainWindow()
{
}

int MainWindow::alpha() const
{
    return m_alpha;
}

void MainWindow::setAlpha(const int a_alpha)
{
    m_alpha = a_alpha;

    QPalette buttonPalette(m_button1->palette());
    QColor buttonColor(buttonPalette.button().color());
    buttonColor.setAlpha(m_alpha);
    buttonPalette.setBrush(QPalette::Button, QBrush(buttonColor));
    m_button1->setPalette(buttonPalette);

    QString stylesheet("background-color: rgba(0,200,0," + QString::number(m_alpha) + ");");
    m_button2->setStyleSheet(stylesheet);

}

ma​​in.cpp:

#include <QtGui/QApplication>

#include "MainWindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow m;
    m.show();

    return app.exec();
}

关于qt:如何使用 QPropertyAnimation 为子 QPushButton 的透明度设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3953382/

相关文章:

javascript - 无限自动滚动动态内容,例如附加

animation - 动画 SVG 路径 d=... 没有描边的坐标

ios - 如何等到动画在 Swift 中完成?

c++ - C++ 的 Qt 超链接

c++ - 将 QString 日期(RFC 822 格式)转换为另一种基于文化的 QString 格式

c++ - 简单的 GUI 应用程序在 Qt 5 中崩溃

带有动画的 Android startActivity

jquery - 如何创建 CSS3 动画 WebkitAnimationEnd 事件数组

c++ - 使用 Qt 和 pjsip 在一个应用程序中的多个端口上监听 SIP

c++ - 防止 Qt 窗口在 Hook 的应用程序中关闭,Eventfilter 不执行任何操作