0

请参阅下面的代码。我有一个启动动画的按钮。问题是我只有在按下 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();
}
4

1 回答 1

1

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

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

#include <QWidget>
#include <QApplication>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QPainter>

#define PropType uint
class Window: public QWidget
{
    Q_OBJECT
    Q_PROPERTY(PropType animProp MEMBER m_animProp NOTIFY animPropChanged)
    PropType m_animProp;
public:
    Window();
    Q_SIGNAL void animPropChanged();
protected:
    void paintEvent(QPaintEvent *);
};

Window::Window()
{
    QPropertyAnimation * anim = new QPropertyAnimation(this, "animProp");
    anim->setDuration( 1000 * 4 );
    anim->setStartValue( 0 );
    anim->setEndValue( 90*100 );
    anim->setLoopCount(-1);

    QPushButton * btn = new QPushButton("Start", this);
    btn->move(20, 20);

    connect(btn, SIGNAL(clicked()), anim, SLOT(start()));
    connect(btn, SIGNAL(clicked()), btn, SLOT(deleteLater()));
    connect(this, SIGNAL(animPropChanged()), SLOT(update()));
}

void Window::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.rotate(m_animProp/100.0);
    p.drawLine(0, 0, width()+height(), 0);
}

int main (int argc, char **argv)
{
    QApplication app(argc, argv);
    Window w;
    w.show();
    return app.exec();
}

#include "main.moc"
于 2013-08-30T20:36:44.610 回答