PDA

View Full Version : scale a QGraphicsView, and it's issues..



been_1990
8th December 2009, 13:52
I tried to use the scale() function available for QGraphicsView. But I cant figure how it exactly works and why I keep getting this weird behavior. I've connected a slider to a function that calls the scale() function of my QGraphicsView. But if you continually slide it up-down, the QGraphicsView will eventually start growing bigger and bigger, or smaller and smaller.:confused:
And besides that the scene rect doesn't scale together with the view(which is expected behavior). How can I scale both?

yogeshgokul
8th December 2009, 14:14
I tried to use the scale() function available for QGraphicsView. But I cant figure how it exactly works and why I keep getting this weird behavior. I've connected a slider to a function that calls the scale() function of my QGraphicsView. But if you continually slide it up-down, the QGraphicsView will eventually start growing bigger and bigger, or smaller and smaller.:confused:
It means you have simply called graphicsView->scale(slider->value(), slider->value()). But this is not a correct way to scale using slider. By this if your slider value is 60, your gv will be scaled 60 times, that you probably never wants.
You should calculate the scale factor and then scale the graphicsView. Means your scale factor should be a real value, simply try this.

qreal sf = 0.0;
sf = slider->value()/10.0;
graphicsView->matrix().reset();
graphicsView->scale(sf, sf);

been_1990
8th December 2009, 14:54
It means you have simply called graphicsView->scale(slider->value(), slider->value()). But this is not a correct way to scale using slider. By this if your slider value is 60, your gv will be scaled 60 times, that you probably never wants.
You should calculate the scale factor and then scale the graphicsView. Means your scale factor should be a real value, simply try this.

qreal sf = 0.0;
sf = slider->value()/10.0;
graphicsView->matrix().reset();
graphicsView->scale(sf, sf);

I just uploaded an attachment with a app that reproduces the problem. Here is the SLOT thats takes care of the scaling:


int tempScale = 0;
void Widget::resizeClock(int t)
{
if(t < tempScale){
clock.scale(.9,.9);
}else if(t > tempScale){
clock.scale(1.1,1.1);
}else{
qDebug() << "equal";
}
tempScale = t;
}

been_1990
8th December 2009, 15:03
And what does matrix->reset() do?

yogeshgokul
9th December 2009, 06:28
And what does matrix->reset() do?
What it seems to do by watching at its name. ;)
I am sure you have reach upto Qt docs.

been_1990
18th December 2009, 00:56
Bad question, I admit...
But still nothing works.. Still resizes at different rates smaller-bigger.

bauervision
14th November 2016, 20:39
Hate to revive such a dead thread, but wanted to post up the solution I found to this question.

void MainWindow::on_ZoomSliderValueChanged(int value)
{
qreal ZoomFactor= 1.01; //real small change
qreal newScale = qPow(ZoomFactor, ZoomFactor);

QMatrix matrix;
matrix.scale(newScale, newScale);

ui->graphicsView->setResizeAnchor(QGraphicsView::AnchorViewCenter);
ui->graphicsView->setMatrix(matrix);
}

Final note, scaling from the center of the view will not happen unless the QGraphicsView has some kind of Layout assigned. No layout, means it will scale from top left. Apply a layout, and it will scale from center.

ChrisW67
14th November 2016, 21:25
Seven years! A new record? ;)

If you scale something by a factor of 2, then 3, then 4, then 3 ... as you move the slider up and down you have scaled by 72 or more very quickly.

You want the slider to give you an absolute scale factor and apply that as the total scaling for the view.

The first part is up to you and how your application should function: for example, a slider over the range -100 to +100 might give you a scale factor over the range 0.01-100.0


double scale = pow(10.0, (slider->value() / 50.0));


The second part is achieved by reseting the view tranformation matrix between scaling calls (if there are no other transforms) or applying the inverse of the existing scale before applying the new scale.