简体   繁体   中英

qt: How to animate the transparency of a QDeclarativeView using QPropertyAnimation?

I want to use QPropertyAnimation to animate the transparency of a QDeclarativeView .But,it doesn't work.It's nothing to change and no errors, but I use property "geometry",it works.I have no ideas.

Here is my main.cpp code:

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

Try to animate widget with using QGraphicsOpacityEffect. Sample:

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

Note: "windowOpacity" property is valid only for widgets that are windows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM