2

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);
  }
}
demonplus
  • 5,613
  • 12
  • 49
  • 68
Palindrom
  • 451
  • 9
  • 26

2 Answers2

1

You can use QObject::sender to get a pointer to the QObject that emitted the signal.

Klathzazt
  • 2,415
  • 19
  • 27
  • Thank you alot.. It is a very useful suggestion. I get the idea behind, but my code also has some fails when I solve it I also update question but sender will done what I want – Palindrom Apr 16 '15 at 21:44
  • Actually, I try sender which is useful but I fail because there is recursion between timer and valuechange. I update code maybe there is a better way to implement – Palindrom Apr 16 '15 at 22:06
  • Use `QObject::blockSignals` to break the recursion. – Pavel Strakhov Apr 16 '15 at 22:26
1

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;
}
Palindrom
  • 451
  • 9
  • 26