Results 1 to 3 of 3

Thread: How to resize a QGraphicsScene to a QGraphicsView?

  1. #1
    Join Date
    Mar 2011
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation How to resize a QGraphicsScene to a QGraphicsView?

    Hi,

    I'm having some trouble to resize the scene to the view size.

    Here is the code:

    bakgroundview.h
    Qt Code:
    1. #ifndef BACKGROUNDVIEW_H
    2. #define BACKGROUNDVIEW_H
    3.  
    4. #include <QObject>
    5. #include <QGraphicsView>
    6. #include <QGraphicsScene>
    7.  
    8. class BackgroundView : public QGraphicsView
    9. {
    10. Q_OBJECT
    11. public:
    12. BackgroundView(QGraphicsView *parent=0);
    13. ~BackgroundView() {};
    14.  
    15. signals:
    16.  
    17. public slots:
    18.  
    19. private:
    20.  
    21. protected:
    22. void resizeEvent(QResizeEvent *event);
    23. };
    24.  
    25. #endif // BACKGROUNDVIEW_H
    To copy to clipboard, switch view to plain text mode 

    bakgroundview.cpp
    Qt Code:
    1. #include <QGraphicsLineItem>
    2. #include <QResizeEvent>
    3.  
    4. #include "backgroundview.h"
    5.  
    6. BackgroundView::BackgroundView(QGraphicsView *parent)
    7. : QGraphicsView(parent)
    8. {
    9.  
    10. scene->setSceneRect(0,0,view->width(),view->height());
    11.  
    12. QGraphicsLineItem* line = new QGraphicsLineItem ( 0,0, scene->width(), 0 );
    13. QGraphicsLineItem* line2 = new QGraphicsLineItem ( scene->width(), 0 , scene->width(), scene->height() );
    14. QGraphicsLineItem* line3 = new QGraphicsLineItem ( scene->width(), scene->height(), 0, scene->height() );
    15. QGraphicsLineItem* line4 = new QGraphicsLineItem ( 0, 0, 0, scene->height() );
    16.  
    17. scene->addItem(line);
    18. scene->addItem(line2);
    19. scene->addItem(line3);
    20. scene->addItem(line4);
    21.  
    22. setScene(scene);
    23. }
    24.  
    25. void BackgroundView::resizeEvent(QResizeEvent *event)
    26. {
    27. //scene->setSceneRect(0, 0, event->size().width(), event->size().height());
    28. QGraphicsView::resizeEvent(event);
    29. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "backgroundview.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8.  
    9. BackgroundView BgView;
    10. BgView.showMaximized();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    When I remove the comment from the scene resize line, the window just close realy fast with an error.

    How can I resize the scene to view every time the view was resized?

    Thanks

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    253
    Thanks
    23
    Thanked 16 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to resize a QGraphicsScene to a QGraphicsView?

    Hi,

    You have an error in your code:

    You have declared the private members in the .h:

    QGraphicsView *view;
    QGraphicsScene *scene;

    But then in the constructor you re-declare them as local:
    QGraphicsView *view = new QGraphicsView();
    QGraphicsScene *scene = new QGraphicsScene();

    Here you did not initialize the private member scene so because you use it in resizeEvent. it crash.

    To fix it:

    You need to change those lines to:
    view = new QGraphicsView(); //Initialize the private member view
    scene = new QGraphicsScene(); //Initialize the private member scene

    Here because you already initialize scene resizeEvent will not crash.

    Carlos

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

    Default Re: How to resize a QGraphicsScene to a QGraphicsView?

    Why do you want to change the scene size when the view resizes? Maybe you want to zoom the scene instead of changing its coordinate range? Because I don't see any reasonable explanation why someone would want to do that.
    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.


Similar Threads

  1. QGraphicsView and QGraphicsScene
    By Molier in forum Qt Programming
    Replies: 11
    Last Post: 28th November 2010, 01:23
  2. QGraphicsView won't update until resize
    By stevel in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2009, 21:45
  3. getting QGraphicsView to resize
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 22nd January 2008, 03:49
  4. QGraphicsScene / QGraphicsView speed after resize
    By themolecule in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2007, 23:46
  5. QGraphicsScene and QGraphicsView
    By rossd in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2007, 14:43

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.