C++ Qt:如何在后台自动执行循环滑块移动?
按下按钮时,让滑块以给定速度在循环中移动的最直接方法是什么?我猜这涉及到分叉线程,该线程定期向滑块发送适当的信号。是否有一个规范的方法来实现这一点?这只是更新滑块在计时器上的位置的问题。因此,创建一个计时器,并在每次更新时调用 当该值为最大值时,将其设置回最小值并继续C++ Qt:如何在后台自动执行循环滑块移动?,c++,qt,C++,Qt,按下按钮时,让滑块以给定速度在循环中移动的最直接方法是什么?我猜这涉及到分叉线程,该线程定期向滑块发送适当的信号。是否有一个规范的方法来实现这一点?这只是更新滑块在计时器上的位置的问题。因此,创建一个计时器,并在每次更新时调用 当该值为最大值时,将其设置回最小值并继续 QSlider* pSlider = new QSlider; QButton * pButton = new QButton("Go"); QTimer* pTimer = nullptr; // C++ 11 nullpt
QSlider* pSlider = new QSlider;
QButton * pButton = new QButton("Go");
QTimer* pTimer = nullptr; // C++ 11 nullptr
// On button click, start a timer
connect(pButton, &QButton::clicked(), [=](){
// exit if already running
if(pTimer)
return;
pTimer = new QTimer;
connect(pTimer, &QTimer::timeout, [=](){
if(pSlider->value()+1 > pSlider->maximum())
pSlider->setValue(pSlider->minimum());
else
pSlider->setValue(++pSlider->value());
});
pTimer->start(1000); // update every second
});
这只是更新滑块在计时器上的位置的问题。因此,创建一个计时器,并在每次更新时调用 当该值为最大值时,将其设置回最小值并继续
QSlider* pSlider = new QSlider;
QButton * pButton = new QButton("Go");
QTimer* pTimer = nullptr; // C++ 11 nullptr
// On button click, start a timer
connect(pButton, &QButton::clicked(), [=](){
// exit if already running
if(pTimer)
return;
pTimer = new QTimer;
connect(pTimer, &QTimer::timeout, [=](){
if(pSlider->value()+1 > pSlider->maximum())
pSlider->setValue(pSlider->minimum());
else
pSlider->setValue(++pSlider->value());
});
pTimer->start(1000); // update every second
});
您可以使用
QTimer
。最简单的例子:
QSlider *sl = new QSlider;
QTimer *ttt = new QTimer;
sl->setValue(0);
connect(ttt,&QTimer::timeout,[=]() {
sl->setValue(sl->value() + 5);
});
sl->show();
ttt->start(500);
我在这里使用了
C++11
(CONFIG+=C++11
到.pro
文件)和,但如果需要,当然可以使用旧语法。可以使用QTimer
。最简单的例子:
QSlider *sl = new QSlider;
QTimer *ttt = new QTimer;
sl->setValue(0);
connect(ttt,&QTimer::timeout,[=]() {
sl->setValue(sl->value() + 5);
});
sl->show();
ttt->start(500);
我在这里使用了
C++11
(CONFIG+=C++11
到.pro
文件)和,但如果您愿意,当然可以使用旧语法。我建议使用QPropertyAnimation
来完成这项工作。只需设置开始值、结束值和要更改值的曲线
QPropertyAnimation *animation = new QPropertyAnimation(slider,"sliderPosition");
//set the duration (how long the animation should run - will change value faster when shorter)
animation->setDuration(1000);
//set the start value - in this case check if value in range of the slider
animation->setStartValue(slider->minimum());
//same as start value
animation->setEndValue(slider->maximum());
//easingCurve defines if it goes straight or bouncing n stuff
animation->setEasingCurve(QEasingCurve::OutCubic);
// as coyote mentioned, you can loop the animation as well (credit him as well ;))
// -1 defines to run forever
animation->setLoopCount(loopCount)
animation->start();
我建议使用
QPropertyAnimation
来完成这项工作。只需设置开始值、结束值和要更改值的曲线
QPropertyAnimation *animation = new QPropertyAnimation(slider,"sliderPosition");
//set the duration (how long the animation should run - will change value faster when shorter)
animation->setDuration(1000);
//set the start value - in this case check if value in range of the slider
animation->setStartValue(slider->minimum());
//same as start value
animation->setEndValue(slider->maximum());
//easingCurve defines if it goes straight or bouncing n stuff
animation->setEasingCurve(QEasingCurve::OutCubic);
// as coyote mentioned, you can loop the animation as well (credit him as well ;))
// -1 defines to run forever
animation->setLoopCount(loopCount)
animation->start();
对我的知识没有规范的方法 其他答案中给出了计时器,但您也可以使用,并通过调整动画的持续时间来调整速度
您可以将loopcount设置为希望动画运行的次数,例如1000000以使其长时间运行。据我所知,没有标准方法 其他答案中给出了计时器,但您也可以使用,并通过调整动画的持续时间来调整速度
您可以将循环计数设置为希望动画运行的次数,例如1000000以使其长时间运行。您还可以更改循环计数以使其不确定地运行您还可以更改循环计数以使其不确定地运行