George Tyrebiter
George Tyrebiter

Reputation: 41

How to do a custom range for a QSpinBox

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.

Upvotes: 0

Views: 3171

Answers (2)

George Tyrebiter
George Tyrebiter

Reputation: 41

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.

Upvotes: 0

headsvk
headsvk

Reputation: 2796

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

Upvotes: 1

Related Questions