Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt:如何在后台自动执行循环滑块移动?_C++_Qt - Fatal编程技术网

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以使其长时间运行。

您还可以更改循环计数以使其不确定地运行您还可以更改循环计数以使其不确定地运行