qt: How to animate the transparency of a QDeclarat

2019-09-16 14:39发布

问题:

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

回答1:

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.



标签: qt animation