Results 1 to 3 of 3

Thread: How to re-scale or changing the size of a QGrapicsScene

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to re-scale or changing the size of a QGrapicsScene

    I have this piece of code:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class GPS_Site : public QGraphicsEllipseItem {
    4. public:
    5. GPS_Site(const QString& text, qreal lon, qreal lat) {
    6. setZValue(2);
    7. QGraphicsEllipseItem::setFlags(ItemIgnoresTransformations | ItemIsSelectable);
    8. QGraphicsEllipseItem::setPos(lon, lat);
    9. QGraphicsEllipseItem::setRect(-5, -5, 10, 10);
    10. label->setFlags(ItemIgnoresTransformations | ItemIsSelectable);
    11. label->setPos(2, 3);
    12. }
    13. };
    14.  
    15. class MainWindow : public QMainWindow {
    16. Q_OBJECT
    17. public:
    18. MainWindow() {
    19. scene->addItem(new GPS_Site("Lower left: A very long text label, bla bla bla bla bla bla bla", 58.111, 24.111));
    20. scene->addItem(new GPS_Site("Upper right: bla bla bla", 58.999, 24.999));
    21. view = new QGraphicsView(scene);
    22. view->setScene(scene);
    23. QMatrix m(1.0, 0.0, 0.0, -1.0, 0.0, 0.0); // identity matrix with reversed Y-axis
    24. view->setMatrix( m );
    25. setCentralWidget(view);
    26. }
    27. protected:
    28. virtual void wheelEvent(QWheelEvent* event) {
    29. qreal scaleFactor = pow((double) 2, -event->delta() / 240.0);
    30. view->scale(scaleFactor, scaleFactor);
    31. }
    32. private:
    33. };
    34.  
    35. int main(int argc, char *argv[]) {
    36. QApplication app(argc, argv);
    37. MainWindow* mainWin = new MainWindow();
    38. mainWin->show();
    39. return app.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    The code, when run, depicts 2 geographical sites with gps coordinates, and labels.
    The sites and labels are made scale invariant using ItemIgnoresTransformations.

    The problem I have is, that the size set by QGraphicsScene is too large, and the reason for this are the long labels used. The rendered scene puts the sites too close to one another.

    What I would like to achieve, is an initial scene size that big enough to encompass the 2 sites (ignoring the label items sizes and positions).

    snapshot1.pngsnapshot3.png
    The first image shows the 1 case, the second shows something what I would like to have.

    I have tried so many combinations of sceneRect, mapFromScene etc etc but with no proper solution.

    Any tips or hints?

    /Regards Kristian
    Last edited by KristianKarl; 9th August 2010 at 12:48.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to re-scale ocr changing the size of a QGrapicsScene

    You can't do much when to positions are that close. You only can zoom in. use a cosmetic painter that the text and circle doesn't change their size.

    EDIT: see QPen::setCosmetic().

  3. #3
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to re-scale ocr changing the size of a QGrapicsScene

    Actually, I found a solution using an article that I found here at QtCentre: Smooth Panning and Zooming

    From that article I wrote:
    Qt Code:
    1. void Layout::FitNetworkInView() {
    2. //Get the rectangle of the visible area in scene coords
    3. QRectF visibleArea = mapToScene( rect() ).boundingRect();
    4.  
    5. //Get map area
    6. QRectF r = myMap->GetMapRect();
    7.  
    8. //Scale the view
    9. qreal scaleFactor = qMin( qAbs( visibleArea.width() / r.width() ), qAbs( visibleArea.height() / r.height() ) );
    10. scaleFactor *= 0.9;
    11. if ( scaleFactor > 0 ) {
    12. scale( scaleFactor, scaleFactor );
    13. } else {
    14. scale( 1.0 / scaleFactor, 1.0 / scaleFactor );
    15. }
    16. SetCenter( r.center() );
    17. }
    To copy to clipboard, switch view to plain text mode 

    Calling the function above will nicely fit my sites within the viewport.
    /Kristian

Similar Threads

  1. Replies: 4
    Last Post: 11th July 2010, 09:47
  2. Replies: 8
    Last Post: 25th April 2010, 21:19
  3. Scale size of QGraphicsItem without scaling pen width
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2009, 00:18
  4. Dynamically changing QGroupBox size
    By T4ng10r in forum Qt Programming
    Replies: 5
    Last Post: 30th March 2007, 15:02
  5. changing the size of the tab width: QTabWidget
    By nikita in forum Qt Programming
    Replies: 2
    Last Post: 29th August 2006, 08:31

Tags for this Thread

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.