请参阅下面的代码。我有一个启动动画的按钮。问题是我只有在按下 SECOND 按钮后才能看到动画。但是当动画开始时,我注意到它实际上从第一次按下按钮开始就开始了,因为它从一个时刻 = 开始[the time between 1 and 2 button press]
。
代码:(抱歉过度杀伤 boost::shared_ptr)
赢赢.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
}
主.cpp:
int main ( int argc, char **argv )
{
QApplication app(argc, argv);
WinWin winwin;
winwin.show();
return app.exec();
}