Results 1 to 3 of 3

Thread: QGraphicsView wrong initial size of objects

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsView wrong initial size of objects

    Hi!

    I'm having trouble with a QGraphicsView. Basically, I want to draw some items (a circle in this example) in a way, that they always fill the complete view.

    I did put the QGraphicsView on the main window using qtcreators designer and use it with the following mainwindow.cpp (full example in as attachement):

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPen>
    4.  
    5. MainWindow::MainWindow(QWidget *parent)
    6. : QMainWindow(parent), ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. circle = new QGraphicsEllipseItem(-10, -10, 20, 20);
    11. circle->setPen(QPen(Qt::black));
    12. scene.addItem(circle);
    13. ui->graphicsView->setScene(&scene);
    14. ui->graphicsView->fitInView(circle, Qt::KeepAspectRatio);
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void MainWindow::resizeEvent(QResizeEvent *e) // virtual
    23. {
    24. ui->graphicsView->fitInView(circle, Qt::KeepAspectRatio);
    25. QMainWindow::resizeEvent(e);
    26. }
    To copy to clipboard, switch view to plain text mode 

    Now what happens is when the window opens, the items do not fill the whole QGraphicsView, they are too small:
    initial_view.png
    But after the first resize event the fitInView function works as expected and the items fill the whole space:
    after_resize.png

    Does somebody have an idea why this happens and how to avoid it?

    Regards,

    syrtyr
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView wrong initial size of objects

    In the constructor the size of the view is not determined yet so fitInView will work on bogus data. You should call fitInView from within showEvent().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    syrtyr (14th July 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGraphicsView wrong initial size of objects

    Thanks! That did the trick!

Similar Threads

  1. Replies: 4
    Last Post: 11th July 2010, 09:47
  2. QGraphicsView and fast moving objects
    By deMarco in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2009, 11:07
  3. How to increase size of QGraphicsView Dynamically
    By Kingofhearts_sri in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2009, 08:54
  4. Problem determining size of QGraphicsView
    By Bocki in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2008, 14:54
  5. Replies: 4
    Last Post: 14th August 2007, 06:56

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.