0

Hi first time posting here. I searched and found how re-implementing the QSpinBox class allows for custom uses. However I am not sure if my needs are addressed in as much as what I found by re-implementing the validate method.

I need a custom range that excludes a zero value in the range of values. The spinner is used for selecting zoom ratio for a loaded image. The initial range at design time is -25 to 10. That range could change depending on the dimensions of the image. Nevertheless, I have to be able to "skip" zero for a desired zoom factor. For example, the range would have to always be going from -1 to 1 or vice-versa.

demonplus
  • 5,613
  • 12
  • 49
  • 68

2 Answers2

1

I assume you're listening to QSpinbox::valueChanged(int i) signal, there you can do something like this:

void zoomImage(int i) {
   if (i == 0) {
      if (lastValue < 0)      //if sliding from negative values
         spinBox->setValue(1);
      else
         spinBox->setValue(-1);

      return;                  //skip processing for 0
   }
   else
      lastValue = i;           //save last state to a class variable

   //processing...
}

EDIT: int lastValue is used for storing the position of slider before it hits 0 in order to determine if the user slides to negative or positive values

headsvk
  • 2,726
  • 1
  • 19
  • 23
  • Thanks for the suggestion. Is the example you posted a slot? It has the same name and signature as the signal valueChanged(int i). I am using the signal and called a slot (zoomImage(int)) from it. This may be better (or at least simpler) than re-implementing QSpinBox methods. Need to capture/cache a means for lastValue – George Tyrebiter Aug 21 '13 at 13:47
  • yeah sure, it's a slot.. you need to know somehow, which direction the user moved the slider in order to skip 0 to -1 or 1, so I used lastValue to remember the last position of the slider before it can reach 0 – headsvk Aug 21 '13 at 13:51
  • Have a solution I think but can't post for a while due to my place in the SO caste system. – George Tyrebiter Aug 21 '13 at 15:51
0

What seems to have worked:

void MainWindow::zoomImage(int ctlValue)
{
   if(ctlValue == 0)
   {
     if(zoomLastValue < 0)
        ui->sbScaleImage->stepBy(1);
     else
        ui->sbScaleImage->stepBy(-1);
   }

   zoomLastValue = ui->sbScaleImage->value();
}

Apologies if I screwed up the formatting.