代码之家  ›  专栏  ›  技术社区  ›  Daud

qt:如何使用qproperties和imation设置子qpushButton的透明度动画?

  •  3
  • Daud  · 技术社区  · 15 年前

    我想在2秒钟内逐步减少qpushbutton的不透明度以完成透明度。为此,我使用了qpropertiesyanimation类,并使用了按钮的属性“windowOpacity”来实现效果。但这只适用于独立的qpushbutton。当我为按钮指定了一个父对象时,效果就消失了。是否有任何方法可以达到儿童按钮的相同效果?

    2 回复  |  直到 13 年前
        1
  •  14
  •   Community CDub    13 年前

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

    标准控件有点问题,而且有很多因素导致了它们的最终外观。您可以采用许多方法,但它们都将涉及一定数量的编码。没有简单的方法:)

    设置 QPushButton ,您需要为它设置样式表,或者更改调色板的一些属性。因为这两个选项都不能被 QPropertyAnimation ,您可以创建自己的自定义属性并对其进行动画处理。

    下面是一些指定自定义属性的代码 MainWindow 打电话 alpha . alpha值用于设置按钮颜色的alpha部分。有了这个属性,我们就可以使用 Q性能和设想 激活它。结果是一个按钮淡入淡出。这只处理按钮的背景,而不是文本,但它应该为您提供一个起点。

    主窗口:

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

    MCP.CPP:

    #include <QtGui/QApplication>
    
    #include "MainWindow.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        MainWindow m;
        m.show();
    
        return app.exec();
    }
    
        2
  •  1
  •   TheSUNSTAR    14 年前

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