我想逐渐降低QPushButton的不透明度,使其在2秒钟内完全透明。为此,我使用了QPropertyAnimation类,并使用按钮的“windowOpacity”属性来实现效果。但是,这仅适用于独立的QPushButton。当我将父级分配给该按钮时,效果消失了。是否有一种方法可以为子按钮实现相同的效果?
windowOpacity
属性只适用于顶级窗口,因此它无法帮助您对子部件的透明度进行动画处理。QPushButton
的透明度,您需要为其设置样式表或更改调色板的一些属性。由于这两个选项都不能直接被QPropertyAnimation
使用,所以您可以创建自己的自定义属性并对其进行动画处理。alpha
的MainWindow
自定义属性的代码。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);
}
main.cpp:
#include <QtGui/QApplication>
#include "MainWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow m;
m.show();
return app.exec();
}
我曾经遇到过同样的问题,并基本上采用了相同的解决方案(操作控件面板)。但是,虽然在MainWindow中的helper属性肯定是一种快速且简单的解决方案,但也是一种不太优雅的解决方案。因此,至少对于更大和经常出现的使用情况,创建一个涵盖这些需求的新动画类似乎更为合适。这并不需要更多的代码(只需继承QAbstractAnimation,将面板相关内容放入其中,并将目标控件作为参数传递给该类),但它可以使您的父控件(如mainwindow-class)摆脱这些动画实现细节,这些细节显然不属于那里。