QPropertyAnimation拒绝在start()调用后立即开始

时间:2013-08-30 19:26:06

标签: c++ qt animation

见下面的代码。我有一个启动动画的按钮。问题是我只能在按下SECOND按钮后看到动画。但是当动画开始时,我注意到自FIRST按钮按下后它实际上已经开始,因为它从片刻开始= [the time between 1 and 2 button press]

代码:(抱歉overkill boost :: shared_ptr)

WinWin.h

#define LOGCOUT(x) { std::cout << __FILE__ << ":" << __LINE__ << " " << x << "\n"; }

class WinWin: public QWidget
{
    Q_OBJECT
    public:
        Q_PROPERTY(unsigned animprop READ getAnimProp WRITE setAnimProp);

        WinWin();
        unsigned getAnimProp();
        void setAnimProp(unsigned);

    protected:
        virtual void paintEvent(QPaintEvent *);

    public slots:
        void slotButtonClicked();

    private:
        unsigned m_anim_prop;
        QPropertyAnimation *m_animation;
        QPushButton *m_button;
};

WinWin.cpp

WinWin::WinWin()
: m_anim_prop(0)
{
    m_animation = new QPropertyAnimation(this, "animprop");
    m_button = new QPushButton(this);
    m_button->setText( "Start" );
    m_button->move ( 40, 40 );
    m_button->resize( 100, 30 );
    m_button->show();

    resize( 640, 480 );
    connect( m_button.get(), SIGNAL(clicked()), this, SLOT(slotButtonClicked()) );
}

unsigned WinWin::getAnimProp()
{

}

void WinWin::setAnimProp(unsigned _prop)
{
    LOGCOUT("anim " << _prop);
    m_anim_prop = _prop;
    update();
}

void WinWin::paintEvent(QPaintEvent *)
{
    QPainter painter( this );
    painter.setPen( QColor(0x00, 0x00, 0x00, 0xff) );
    painter.drawLine( 0, 0, width(), m_anim_prop );
}

void WinWin::slotButtonClicked()
{
    m_animation->setDuration( 1000 * 4 );
    m_animation->setStartValue( 0 );
    m_animation->setEndValue( height() );
    m_animation->setLoopCount( -1 ); // infinite number of loops

    // Fire!
    m_animation->start();

    LOGCOUT( m_animation->state() ); // says: 2
}

的main.cpp

int main ( int argc, char **argv )
{
    QApplication app(argc, argv);

    WinWin winwin;
    winwin.show();

    return app.exec();
}

1 个答案:

答案 0 :(得分:1)

出于某种原因,它使用无符号整数类型。只需使用有符号整数。这可能是一个错误,它似乎存在于Qt 4.8和5.1中。它至少在Qt 5.5中修复。

下面的代码显示了如何在Qt 5.1中最低限度地完成它。由于MEMBER的{​​{1}}字段的使用,它在Qt 4中不起作用,但它表明在Qt 5.1中事情变得更好了:)

Q_PROPERTY