0
votes

I have a problem with scaling an image in QT. I have a GraphicsView that has an image displayed and I have put in a slider to be able to allow the user to zoom in and out. For this, I've been using the scale function of the QTransform class to achieve this. It generally works if I zoom in, ie, if value gets bigger and bigger. However, when I go the opposite direction (making value smaller and smaller), the entire image disappears. Zooming back in doesn't fix the problem either; the entire image is no longer present no matter how much I zoom in or out. So the following are snippets of my code.

void MainWindow::on_ZoomSlider_valueChanged(int value)
{
  QTransform transformer;
  transformer.scale(scaler(value),scaler(value));
  ui->graphicsView->setTransform(transformer);
}

qreal MainWindow::scaler(int value)
{
    if(value < 0)
        return  1/(-(value-1));
    else if(value >0)
        return value+1;
    else
        return value+1;
}

I'd be happy to offer any more clarification on my problem if it'd help you help me figure out what's wrong.

1

1 Answers

2
votes

The 1/(-(value-1)) expression evaluates as an INTEGER, which is always zero in your case. Try using 1.0/(-(value-1)). Also, I'm not really familiar with sliders, but I don't understand why could the value be negative. The valueChanged signal comes with the actual value, not the delta.