[Qt-interest] Fit the Geometry on the screen
Sean Harmer
sean.harmer at maps-technology.com
Tue Jun 23 10:11:06 CEST 2009
Hi,
On Tuesday 23 Jun 2009 08:58:50 Sujan Dasmahapatra wrote:
> ////////////////////////////////////////////////////////////////////////
> /////////////////////////////////
> QGraphicsView *view = new QGraphicsView;
> QGraphicsItem *geometry = new QGraphicsItem;
> if(translateRadioButton->isChecked()==true) {
> QString dxString = dxLineEdit->text();
> QString dyString = dyLineEdit->text();
> double dx = dxString.toDouble(); // for translation
> double dy = dyString.toDouble();
> geometry->translate(dx,dy);
> }
> else if(rotationRadioButton->isChecked()==true) {
> QString angleString = angleLineEdit->text();
> QString xcString = xcLineEdit->text();
> QString ycString = ycLineEdit->text(); // for rotation
> double angle = angleString.toDouble();
> double xc = xcString.toDouble();
> double yc = ycString.toDouble();
> geometry->setTransform(QTransform().translate(xc,
> yc).rotate(angle).translate(-xc, -yc));
> }
> else if(scalingRadioButton->isChecked()==true) {
> QString factorString = factorLineEdit->text();
> double factor = factorString.toDouble(); // for scaling
> geometry->scale(factor,factor);
> }
> else
> statusBar()->showMessage(tr("No transformation"),2000)
>
> //slot for fitting the graphicsItem on the screen.
> fitInviewFunction()
> {
> View->fitInView(geometry,Qt::KeepAspectRatio);
> }
> ////////////////////////////////////////////////////////////////////////
> //////////////////////////////////
> ////////////////////////////////////////////////////////////////////////
> //////////////////////////////////
> Please check the rotation and scaling part if its correct and also
> fitInView() part....
Well we are getting closer. That is not a compilable example but at least it
gives an idea of what you are trying to do.
Things to check:
1). Which code path (type of transform) is causing the problem?
2). Have you tried playing around with the transform parameters to see what
happens as you gradually increase the parameters? Does the item slowly go out
of view as you increase the parameters or does it disappear even for small
values?
3). Are you *sure* that your slot fitInViewFunction() is really being called?
If so at what point is it called?
4). The docs for QGraphicsView::fitInView() say this:
"Scales the view matrix and scrolls the scroll bars to ensure that the scene
rectangle rect fits inside the viewport. rect must be inside the scene rect;
otherwise, fitInView() cannot guarantee that the whole rect is visible."
Are you setting an explicit scene rect somewhere in your code? If you are and
your transformed item lies outside it then fitInView will not work. You would
have to adjust your scene rect in this case.
If you do not explicitly set a scene rect then the GraphicsView framework will
automatically grow it as needed and you will get scroll bars appear in your
view widget that allow you to scroll around the full extent of your scene. Do
you get scroll bars?
Sean
More information about the Qt-interest-old
mailing list