Results 1 to 9 of 9

Thread: QGraphicsView and QGraphicsScene

  1. #1
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsView and QGraphicsScene

    Hi,

    I have a problem about setting the rectangle size of the QGraphicsScene to QGraphicsView's visible part.

    When I set the scene's rectangle using view's width and height, the scroll bars appear.

    How can I determine the maximum visible size of the QGraphicsView so that I can set the scene's rectangle to maximum ?

    The example code is below to demonstrate the problem:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QDebug>
    4.  
    5. int main ( int argc, char *argv[] )
    6. {
    7. QApplication app ( argc, argv );
    8.  
    9. view.setGeometry ( 10, 10, 400, 300 );
    10. view.setScene ( new QGraphicsScene() );
    11.  
    12. qint32 gvWidth = view.width();
    13. qint32 gvHeight = view.height();
    14. view.setSceneRect ( -gvWidth / 2, -gvHeight / 2, gvWidth, gvHeight );
    15.  
    16. QGraphicsLineItem* line = new QGraphicsLineItem ( - gvWidth / 2, - gvHeight / 2, gvWidth / 2, gvHeight / 2 );
    17. view.scene()->addItem ( line );
    18.  
    19. view.show();
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    Sami

  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 and QGraphicsScene

    Use the viewport() rect of the view.

  3. #3
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView and QGraphicsScene

    Actually, I have figured out that there is a problem about the viewport's size when using QMainWindow.

    I have created a QMainWindow with a QMenuBar and a QGraphicsView on the central part of the QMainWindow.

    When the QMenuBar is created and the `setMenuBar()` method of QMainWindow is called, the size of the resulting viewport of the view is not the same as the viewport's size (there is a 20-30 pixels difference). But if `setMenuBar()` method is not called, everything works just fine.

  4. #4
    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 and QGraphicsScene

    Are you sure it is not your fault? Maybe you are reading the size too early or something? I strongly doubt some external widget would have any influence on some other widget's internal widget.

  5. #5
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView and QGraphicsScene

    wysota,

    I hope it is my fault, as I couldn't figure out how to fix this problem.

    Below are the files for the case. When commenting out the below line in QGuiMainScreen.cpp, the scrollbars disappears:
    Qt Code:
    1. this->setMenuBar ( menuBar );
    To copy to clipboard, switch view to plain text mode 

    QGuiMainScreen.h
    Qt Code:
    1. #include <QMainWindow>
    2. #include <QMenuBar>
    3. #include <QGraphicsView>
    4.  
    5. class QGuiMainScreen: public QMainWindow
    6. {
    7. public:
    8. QGuiMainScreen();
    9. virtual void show();
    10.  
    11. private:
    12. };
    To copy to clipboard, switch view to plain text mode 

    QGuiMainScreen.cpp
    Qt Code:
    1. #include "QGuiMainScreen.h"
    2.  
    3. #include <QLayout>
    4. #include <QGLWidget>
    5.  
    6. QGuiMainScreen::QGuiMainScreen() : QMainWindow()
    7. {
    8. QWidget *centralwidget;
    9. QHBoxLayout *outerLayout;
    10. QMenuBar *menuBar;
    11. QMenu *menuSystem;
    12.  
    13. this->resize ( 1024, 768 );
    14. centralwidget = new QWidget ( this );
    15. this->setCentralWidget ( centralwidget );
    16.  
    17. menuBar = new QMenuBar ( this );
    18. menuBar->setGeometry ( QRect ( 0, 0, 1024, 30 ) );
    19. menuSystem = new QMenu ( menuBar );
    20. menuSystem->setTitle ( "System" );
    21. menuBar->addAction ( menuSystem->menuAction() );
    22. this->setMenuBar ( menuBar );
    23.  
    24. outerLayout = new QHBoxLayout ( centralwidget );
    25. outerLayout->setSpacing ( 0 );
    26. outerLayout->setMargin ( 0 );
    27.  
    28. m_gv = new QGraphicsView ( this );
    29. m_gv->setScene ( new QGraphicsScene() );
    30. outerLayout->insertWidget ( 0, m_gv, 1 );
    31. }
    32.  
    33. void QGuiMainScreen::show()
    34. {
    35. QMainWindow::show();
    36.  
    37. m_gv->setViewport ( new QGLWidget() );
    38.  
    39. qint32 sceneWidth = m_gv->viewport()->width();
    40. qint32 sceneHeight = m_gv->viewport()->height();
    41.  
    42. m_gv->scene()->setSceneRect ( -sceneWidth / 2, -sceneHeight / 2, sceneWidth, sceneHeight );
    43. }
    To copy to clipboard, switch view to plain text mode 

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

    pro file
    Qt Code:
    1. TEMPLATE = app
    2. TARGET =
    3. DEPENDPATH += .
    4. INCLUDEPATH += .
    5. QT += opengl
    6.  
    7. # Input
    8. HEADERS += QGuiMainScreen.h
    9. SOURCES += main.cpp QGuiMainScreen.cpp
    To copy to clipboard, switch view to plain text mode 

    Sami

  6. #6
    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 and QGraphicsScene

    First of all you don't have to create a menu bar yourself. If you call QMainWindow::menuBar() a menu bar will be created and setup for you. There is a chance this will already fix your problem.

  7. #7
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView and QGraphicsScene

    Actually, the code is generated by uic and I just copied the generated code to demonstrate the problem.

    So when I call `setupUi()` the problem occurs.

  8. #8
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView and QGraphicsScene

    wysota,

    I also tried to create the menubar by using QMainWindow::menuBar() but again the viewport do not have the correct size and the problem persists.

    sami

  9. #9
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView and QGraphicsScene

    Hi,

    It is confirmed by Qt support that this is bug related to the menubar.

    Yeah, looks like this is a bug because QMenuBar is reporting it's size wrong as a height of 2 pixels instead of 30 for some reason.
    wysota,
    Thanks for your interest.

Similar Threads

  1. QGraphicsView, QGraphicsItem, QGraphicsScene
    By Shuchi Agrawal in forum Newbie
    Replies: 10
    Last Post: 23rd March 2011, 20:50
  2. Embed QGraphicsView in the QGraphicsScene
    By maverick_pol in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2008, 08:40
  3. QGraphicsScene and QGraphicsView
    By sabeesh in forum Qt Programming
    Replies: 7
    Last Post: 1st August 2007, 06:59
  4. QGraphicsScene and QGraphicsView
    By rossd in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2007, 14:43
  5. QGraphicsView and QGraphicsScene speeds
    By jnk5y in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2006, 07:13

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.