Results 1 to 8 of 8

Thread: scale a QGraphicsView, and it's issues..

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default scale a QGraphicsView, and it's issues..

    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.
    And besides that the scene rect doesn't scale together with the view(which is expected behavior). How can I scale both?
    Attached Files Attached Files
    Last edited by been_1990; 8th December 2009 at 14:42.

  2. #2
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: scale a QGraphicsView, and it's issues..

    Quote Originally Posted by been_1990 View Post
    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.
    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.
    Qt Code:
    1. qreal sf = 0.0;
    2. sf = slider->value()/10.0;
    3. graphicsView->matrix().reset();
    4. graphicsView->scale(sf, sf);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: scale a QGraphicsView, and it's issues..

    Quote Originally Posted by yogeshgokul View Post
    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.
    Qt Code:
    1. qreal sf = 0.0;
    2. sf = slider->value()/10.0;
    3. graphicsView->matrix().reset();
    4. graphicsView->scale(sf, sf);
    To copy to clipboard, switch view to plain text mode 
    I just uploaded an attachment with a app that reproduces the problem. Here is the SLOT thats takes care of the scaling:
    Qt Code:
    1. int tempScale = 0;
    2. void Widget::resizeClock(int t)
    3. {
    4. if(t < tempScale){
    5. clock.scale(.9,.9);
    6. }else if(t > tempScale){
    7. clock.scale(1.1,1.1);
    8. }else{
    9. qDebug() << "equal";
    10. }
    11. tempScale = t;
    12. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: scale a QGraphicsView, and it's issues..

    And what does matrix->reset() do?

  5. #5
    Join Date
    Dec 2007
    Posts
    627
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: scale a QGraphicsView, and it's issues..

    Quote Originally Posted by been_1990 View Post
    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.

  6. #6
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: scale a QGraphicsView, and it's issues..

    Bad question, I admit...
    But still nothing works.. Still resizes at different rates smaller-bigger.

  7. #7
    Join Date
    Nov 2016
    Posts
    2
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: scale a QGraphicsView, and it's issues..

    Hate to revive such a dead thread, but wanted to post up the solution I found to this question.

    void MainWindow:n_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.
    Last edited by bauervision; 14th November 2016 at 21:31. Reason: updated contents

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,590
    Thanks
    13
    Thanked 1,586 Times in 1,515 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: scale a QGraphicsView, and it's issues..

    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
    Qt Code:
    1. double scale = pow(10.0, (slider->value() / 50.0));
    To copy to clipboard, switch view to plain text mode 

    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.

  9. The following user says thank you to ChrisW67 for this useful post:

    bauervision (14th November 2016)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.