0

我想使用 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();
}
4

1 回答 1

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”属性仅对窗口小部件有效。

于 2013-07-10T09:18:38.583 回答