QT:如何用动画一个QPropertyAnimation孩子QPushButton的透明度? [英] qt: How to animate the transparency of a child QPushButton using QPropertyAnimation?

查看:2181
本文介绍了QT:如何用动画一个QPropertyAnimation孩子QPushButton的透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲逐渐减小QPushButton的不透明度超过2秒的时间来完成的透明度。对于我使用的QPropertyAnimation类,并使用该按钮的财产windowOpacity达到的效果。但是,只工作了一个独立的QPushButton。当我分配父到按钮,效果就消失了。有没有实现对孩子的按钮同样的效果的方法吗?

I want to progressively decrease the opacity of a QPushButton over a time of 2 seconds to complete transparency. For that I used the QPropertyAnimation class and used the property "windowOpacity" of the button to achieve the effect. But that worked only for a standalone QPushButton. When I assigned a parent to the button, the effect disappeared. Is there any way of achieving the same effect for child buttons ?

推荐答案

windowOpacity 属性仅适用于顶级窗口,所以它不会帮你的动画透明度孩子不幸小部件

The windowOpacity property only applies to top level windows so it won't help you with animating transparency on child widgets unfortunately.

标准控件是有点问题的,以及有许多注意事项有利于他们的决赛。还有你可以采取许多方法,但他们都将涉及一定量的编码。有没有简单的方法:)

Standard controls are a bit problematic as well as there are many considerations contributing to their final appearance. There are many approaches you could take but they will all involve a certain amount of coding. There is no easy way :)

要设置透明度的 QPushButton ,你需要或者设置一个样式表,或者改变一些调色板的属性。由于没有这些选项是通过直接使用 QPropertyAnimation ,你可以创建自己的自定义属性和动画的。

To set the transparency of a QPushButton, you would need to either set a stylesheet for it, or change some of the properties of the palette. Since neither of these options are directly usable by a QPropertyAnimation, you can create your own custom property and animate that.

下面是一些code,指定自定义属性主窗口名为字母。阿尔法值用于设置按钮颜色的alpha部。与此属性的地方,我们可以使用 QPropertyAnimation 动画吧。其结果是一个按钮,在进出变淡。这仅处理的按钮背景而不是文本,但它应该为你提供一个起始点。

Below is some code that specifies a custom property for a MainWindow called alpha. The alpha value is used to set the alpha portion of the button color. With this property in place, we can use QPropertyAnimation to animate it. The result is a button that fades in and out. This only handles the buttons background and not the text but it should provide a starting point for you.

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

这篇关于QT:如何用动画一个QPropertyAnimation孩子QPushButton的透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆