【问题标题】:qt: How to animate the transparency of a child QPushButton using QPropertyAnimation?qt:如何使用 QPropertyAnimation 为子 QPushButton 的透明度设置动画?
【发布时间】:2010-10-17 13:27:39
【问题描述】:

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

【问题讨论】:

    标签: qt animation


    【解决方案1】:

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

    【讨论】:

    • 非常感谢。它有效,但如果我通过样式表设置颜色则无效。关于操作哪些函数以使边框和文本也透明的任何提示?
    • QPalette 的文档中提供了您应该需要的一切。那里有很多,但是阅读和试验我提供的代码会让你到达那里。这就是我所做的一切:)
    • 我已更新示例代码以包含一个具有基于样式表的透明度的按钮,因为您提到您无法让它工作。
    【解决方案2】:

    前段时间我遇到了同样的问题,并得出了基本相同的解决方案(操作控件调色板)。但是,虽然 MainWindow 中的 helper 属性肯定是一个快速简单的解决方案,但它也是一个肮脏的解决方案。所以,至少对于更大的和重复出现的使用来说,创建一个满足这些需求的新动画类更合适。这不是更多代码(只需继承 QAbstractAnimation,将调色板内容移到那里并将目标控件作为参数传递给该类),但它使您的父控件(如主窗口类)不受此类动画实现细节的影响肯定不属于那里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 2011-01-11
      • 2015-03-15
      • 1970-01-01
      • 2013-04-21
      • 2012-05-31
      • 2013-07-17
      相关资源
      最近更新 更多