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

时间:2013-07-10 02:53:08

标签: qt animation

我想使用QPropertyAnimation来动画QDeclarativeView的透明度。但是,它不起作用。没有什么可以改变而且没有错误,但我使用属性“几何”,它有效。我没有想法。

这是我的main.cpp代码:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv, true);
    QDeclarativeView* view;
    QDeclarativeContext *context;
    QDeclarativeEngine* engine;
    Connector* connector;

    view = new QDeclarativeView();

    connector = new Connector();
    context = view->rootContext();
    context->setContextProperty("Connector", connector);


    context->setContextProperty("gRadioQMLDir", QDir::currentPath());
    view->setSource(QUrl::fromLocalFile("qml/Main.qml"));
    view->setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);

   view->show();
   QPropertyAnimation animation(view, "windowOpacity");
   animation.setDuration(30000);
   animation.setStartValue(0.0);
   animation.setEndValue(1.0);

   animation.start();
     return a.exec();
}

1 个答案:

答案 0 :(得分:1)

尝试使用QGraphicsOpacityEffect为小部件设置动画。样品:

#include <QApplication>
#include <QWidget>
#include <QDeclarativeView>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QPushButton>

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

    QWidget w;
    QGraphicsOpacityEffect *opacity = new QGraphicsOpacityEffect;
    QPropertyAnimation *anim = new QPropertyAnimation( opacity, "opacity" );

    //QDeclarativeView *dv = new QDeclarativeView( &w );
    //dv->setFixedSize( 200, 200 );
    //dv->setGraphicsEffect( opacity );
    QPushButton *btn = new QPushButton( "Button", &w );
    btn->setGraphicsEffect( opacity );

    anim->setDuration( 2000 );
    anim->setStartValue( 0.1 );
    anim->setEndValue( 1.0 );
    anim->setEasingCurve( QEasingCurve::InCubic );
    anim->start();

    w.setFixedSize( 300, 200 );
    w.show();

    return a.exec();
}

注意:“windowOpacity”属性仅对窗口小部件有效。