Search code examples
c++qtopengltimerqslider

QSlider , QTimer and valueChanged usage


I try to use QSlider, QTimer and valueChanged() signal together but I need to differentiate whether value of slider is changed by user or timer tick. How can I accomplish this? By below code I try to decide when slider is changed by timer but I could not decide when signal changed by user. ( Moreover it is a OpenGL animation and slider behaves like timeline evenif timeline changes value every second animation plays 30 Hz therefore if user want to use slider for making animation forward or reverse I need to check signal of slider. However slider has one seconds ticks from timer)

connect(timer, SIGNAL(timeout()), this,SLOT(timerTick()));
connect(slider, SIGNAL(valueChanged(int)),this, SLOT(sliderChange()));

void MainWindow::sliderChange()
{
  if (userInterrupt)
  {
   .. Call Function A
  }
}
void MainWindow::timerTick()
{
  slider->setValue(slider.value()+1);
  userInterrupt=false;
}

EDIT : sender is added but due recursion it is fail to run clearly. Still I could not decide signal

 connect(timer, SIGNAL(timeout()), this,SLOT(sliderChange()));
 connect(slider, SIGNAL(valueChanged(int)),this, SLOT(sliderChange()));

void MainWindow::sliderChange()
{
 QObject * obj =sender();
 if (obj==slider)
  {
   .. Call Function A
  }else
  {
  slider->setValue(slider.value()+1);
  }
}

Solution

  • After I try sender and blocksignals, I could not manage to solve the issue. Therefore, I find out another more primitive solution on slider handler like below. However, still I think that sender and blocksignal is better way to solve and try to do in that way also, until that time below code solve my issue. Basically, I use different signals for release, click and drag on slider.

    connect(timer, SIGNAL(timeout()), this,SLOT(timerTick()));
    connect(slider, SIGNAL(valueChanged(int)),this, SLOT(sliderChange()));
    connect(slider, SIGNAL(sliderReleased()),this, SLOT(userRelease()));
    connect(slider, SIGNAL(sliderPressed()),this, SLOT(userClick()));
    
    void MainWindow::sliderChange()
    {
     // Action when it is changes
    // in my case calculate time where animation will resume on
    // but do not show any data on animation until release
    // release slot will deal with it
    }
    
    void MainWindow::userClick()
    {
     // Actions when mouse button is pressed 
    // in my case pause animation
    }
    void MainWindow::userRelease()
    {
    // Action when mouse button is released
    // in my case resume showing animation with slider value
    } 
    void MainWindow::timerTick()
    {
      slider->setValue(slider.value()+1);
      userInterrupt=false;
    }