Results 1 to 20 of 21

Thread: QGraphicsView scale question.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scale question.

    No, I believe you have to use scene coords instead of view coords. After all, you are trying to center the scene.

    BTW: what app are you working on? some kind of photoshop? looks pretty good... especially the tabs in the upper control bar.

    Regards.

  2. #2
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView scale question.

    Please see previous post i edited with some screenshots.

    So as you say, my misstake is probably the referential. I'll try to look at this more further soon, but for now i have lost the faith

    What i'm trying to do is actually a plugin for Photoshop. What you see is actually Photoshop
    We decided to use the QT commercial edition for the gui, since Photoshop exists on Mac and since i worked on QT for some opensource stuff, i was able to make the financials of this company buy QT ^^

    Pierre.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scale question.

    Yes... I didn't realize it was photoshop... The plugin looks good.

    Use scene coordinates for centerOn and do not take into account the zoom factor. Just use:

    Qt Code:
    1. view->centerOn( itemCenterPosInSceneCoords ).
    To copy to clipboard, switch view to plain text mode 


    Because if you scale only the item, the scale factor of the scene will not change. You will just have a bigger( smaller ) item, depending on the zoom.

  4. #4
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView scale question.

    Yeah but to get what you call the "item center in the scene", how am i supposed to do, since i cant get the item's width / height?
    i would do centerOn(QPoint(item->x()+item->Width()/2, item->y()+item->Height()/2)), but there are not such methods width() height()! So how do i get the item's center?
    Pseudo code please, i'm totally lost

    Pierre.

    [EDIT:] Maybe you should just try this: create a graphics view, put a big QImage in it. Then tell the image to scale down, and try to center it. Also when the picture is big, it is draggable off-view.
    Last edited by hickscorp; 14th April 2007 at 19:14.

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scale question.

    Ok. I have played a little bit with QGraphicsView/Scene.

    Here's the code:

    Qt Code:
    1. QGraphicsItem* itm = mDocument->getCurrentPage()->items()[0];
    2. QRectF f= itm->boundingRect();
    3. QPointF mapped1 = itm->mapToScene( f.topLeft() );
    4. QPointF mapped2 = itm->mapToScene( f.bottomRight() );
    5. centerOn( ( mapped2.x() - mapped1.x() )/2, ( mapped2.y() - mapped1.y() )/2 );
    To copy to clipboard, switch view to plain text mode 
    That's it...
    This is done in a QGraphicsView class...
    I guess you can do it wherever you need it.

    NOTE: When you scale an item, the top left position always remains the same.

    Regards.

  6. The following user says thank you to marcel for this useful post:

    hickscorp (15th April 2007)

  7. #6
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView scale question.

    Hello sorry for the delay

    With your code, i have the very same problem. The item is "stuck" to the top left corner, and only can be dragged off-screen when it is little. When it's bigger than the viewable area, it's draggable but can be sragged off-screen in the top left direction too.

    There comes a preview of my code:

    Qt Code:
    1. void CDlgSettingsImpl::on_btnZoomIn_clicked(void) {
    2. if (iZoomFactor>=1600)
    3. return;
    4. iZoomFactor *= 2;
    5. lblZoomLevel->setText(QString::number(iZoomFactor)+"%");
    6.  
    7. QGraphicsItem* pPicture;
    8. QPointF mapped1, mapped2;
    9. QRectF f;
    10.  
    11. pPicture = grphVwOriginalPicture->items().first();
    12. pPicture->scale(2, 2);
    13. f = pPicture->boundingRect();
    14. mapped1 = pPicture->mapToScene(f.topLeft());
    15. mapped2 = pPicture->mapToScene(f.bottomRight());
    16. grphVwOriginalPicture->centerOn((mapped2.x()-mapped1.x())/2, (mapped2.y()-mapped1.y())/2);
    17. }
    18. void CDlgSettingsImpl::on_btnZoomOut_clicked(void) {
    19. if (iZoomFactor<=25)
    20. return;
    21. iZoomFactor /= 2;
    22. lblZoomLevel->setText(QString::number(iZoomFactor)+"%");
    23.  
    24. QGraphicsItem* pPicture;
    25. QPointF mapped1, mapped2;
    26. QRectF f;
    27.  
    28. pPicture = grphVwOriginalPicture->items().first();
    29. pPicture->scale(.5, .5);
    30. f = pPicture->boundingRect();
    31. mapped1 = pPicture->mapToScene(f.topLeft());
    32. mapped2 = pPicture->mapToScene(f.bottomRight());
    33. grphVwOriginalPicture->centerOn((mapped2.x()-mapped1.x())/2, (mapped2.y()-mapped1.y())/2);
    34. }
    To copy to clipboard, switch view to plain text mode 

    Can it be a bug with my QT installation? i havnt tested any of the QGraphics framework on linux, so i'm not sure if it's windows only ...

    Pierre.

    [EDIT:] Maybe the error is here, so here comes how i create the QGraphicsScene & View stuff (In the dialog's constructor):
    Qt Code:
    1. // Create the original preview objects.
    2. grphScOriginal = new QGraphicsScene(this);
    3. grphVwOriginalPicture = new QGraphicsView(grphScOriginal, this);
    4. grphVwOriginalPicture->setFrameShape(QFrame::Panel);
    5. grphVwOriginalPicture->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    6. grphVwOriginalPicture->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    7. grphVwOriginalPicture->setDragMode(QGraphicsView::ScrollHandDrag);
    8. grphVwOriginalPicture->setBackgroundBrush(Qt::darkGray);
    9. fillPreviewFromPlane(grphScOriginal, pInPlane);
    10.  
    11. void CDlgSettingsImpl::fillPreviewFromPlane(QGraphicsScene* grphScToFill, CARGBPlane* pPlane) {
    12. QList<QGraphicsItem*> lstItems = grphScToFill->items();
    13. while (!lstItems.isEmpty()) {
    14. QGraphicsItem* pCurrentItem = lstItems.takeFirst();
    15. grphScToFill->removeItem(pCurrentItem);
    16. delete pCurrentItem;
    17. }
    18. QImage cImage = QImage(pPlane->Width(), pPlane->Height(), QImage::Format_RGB32);
    19. byte* fR = pPlane->fR;
    20. byte* fG = pPlane->fG;
    21. byte* fB = pPlane->fB;
    22. for (int iY=0; iY<pPlane->Height(); iY++)
    23. for (int iX=0; iX<pPlane->Width(); iX++)
    24. cImage.setPixel(iX, iY, qRgb(*fR++, *fG++, *fB++));
    25. grphScToFill->addPixmap(QPixmap::fromImage(cImage, Qt::ColorOnly | Qt::ThresholdDither));
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by hickscorp; 15th April 2007 at 00:26.

  8. #7
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView scale question.

    I believe i have found the "why". In the documentation, it is said this:
    sceneRect : QRectF
    This property holds the scene rectangle; the bounding rectangle of the scene.
    The scene rectangle defines the extent of the scene. It is primarily used by QGraphicsView to determine the view's default scrollable area, and by QGraphicsScene to manage item indexing.
    If unset, or if set to a null QRectF, sceneRect() will return the largest bounding rect of all items on the scene since the scene was created (i.e., a rectangle that grows when items are added to or moved in the scene, but never shrinks).


    Which means, if my scene is created containing a QImage of 1600x1200 and if i scale down this image to 800x600, the sceneRect will still be 1600x1200. So i just tried to do a "grphScProcessed->setSceneRect(pPicture->boundingRect());" right after my item is scaled down, but same problem. Any clue please?

    Pierre.

  9. #8
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView scale question.

    ok i think i have finally found what i needed. Here is what i do now, right after changing the scale of my item in the scene:

    Qt Code:
    1. pPicture = grphVwOriginalPicture->items().first();
    2. pPicture->scale(.5, .5);
    3. cTopLeft = pPicture->mapToScene(pPicture->boundingRect().topLeft());
    4. cBotRight = pPicture->mapToScene(pPicture->boundingRect().bottomRight());
    5. grphScOriginal->setSceneRect(cTopLeft.x(), cTopLeft.y(), cBotRight.x()-cTopLeft.x(), cBotRight.y()-cTopLeft.y());
    To copy to clipboard, switch view to plain text mode 

    It seems it's sufficient to set up the scene's view rect to the right rect...

    Marcel, thanks for all your help, it's because of you i finally understood what the mappedTo / mappedFrom methods do

    Pierrre.

  10. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView scale question.

    I was just looking over your last 2 posts...
    Good you found a solution...

    BTW:
    setSceneRect( item->boundingRect( )) did not work because boundingRect remains unscaled. It always remains at the original value. That is why the mapping is needed.

    Another thing to remember is that when you create the raster it is placed in the scene at position (0,0).
    When you scale it, it will shrink, but the top left position is still at (0, 0), that is why the center would not work. I thought that is what you wanted - to see the entire scaled picture, not to see it AND be centered .

    Your solution, set scene rect will shrink the scene size, but I guess that's ok, because you'll set it again if you zoom in/out...

    Regards

Similar Threads

  1. Replies: 1
    Last Post: 15th March 2007, 20:45
  2. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  3. Question regarding how to paint after zoom.
    By JonathanForQT4 in forum Qt Programming
    Replies: 2
    Last Post: 26th January 2007, 15:34
  4. Efficient Scaling and Rotating of QGraphicsView
    By forrestfsu in forum Qt Programming
    Replies: 10
    Last Post: 12th December 2006, 16:28
  5. QGraphicsView scale function
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 12th October 2006, 14:05

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
  •  
Qt is a trademark of The Qt Company.