gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:27:17 26 4
gpt4 key购买 nike

请参阅下面的代码。我有一个启动动画的按钮。问题是我只在按下第二个按钮后才看到动画。但是当动画开始时,我注意到它实际上从第一次按下按钮开始就开始了,因为它从一个时刻开始 = [第 1 次和第 2 次按钮按下之间的时间]

代码:(抱歉,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
}

ma​​in.cpp:

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

WinWin winwin;
winwin.show();

return app.exec();
}

最佳答案

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

下面的代码展示了如何在 Qt 5.1 中最少地完成它。由于使用了 Q_PROPERTYMEMBER 字段,它在 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"

关于c++ - QPropertyAnimation 拒绝在 start() 调用后立即开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18540151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com