Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-3244

Size of QGraphicsView::viewport() thrown off by QMenubar

    XMLWordPrintable

Details

    Description

      This example application uses the reported size of the viewport to create a scene of the same size such that nothing is scaled and there are no Scrollbars. This normally works, but when a QMenuBar is added to the viewports reports it's size to be bigger than it actually is leading to a scene larger than the viewport.

      The debug statements in this program suggest than the QMenuBar reports itself to only be 2 pixels high. The other widgets on the form adjust themselves to accommodate for 2 pixels but since this is inaccurate this leads to the wrong size being reported by viewport of itself.

      #include <QApplication>

      #include <QMainWindow>
      #include <QMenuBar>
      #include <QGraphicsView>

      class QGuiMainScreen: public QMainWindow
      {
      public:
      QGuiMainScreen();
      virtual void show();

      private:
      QGraphicsView* m_gv;
      };

      #include <QLayout>
      #include <QGLWidget>
      #include <QDebug>

      QGuiMainScreen::QGuiMainScreen() : QMainWindow()
      {
      QWidget *centralwidget;
      QHBoxLayout *outerLayout;
      QMenuBar *menuBar;
      QMenu *menuSystem;

      this->resize ( 1024, 768 );
      centralwidget = new QWidget ( 0 );
      this->setCentralWidget ( centralwidget );

      menuBar = new QMenuBar ( 0 );
      menuBar->setGeometry ( QRect ( 0, 0, 1024, 30 ) );
      menuSystem = new QMenu ( menuBar );
      menuSystem->setTitle ( "System" );
      menuBar->addAction ( menuSystem->menuAction() );
      this->setMenuBar ( menuBar );

      outerLayout = new QHBoxLayout ( centralwidget );
      outerLayout->setSpacing ( 0 );
      outerLayout->setMargin ( 0 );

      m_gv = new QGraphicsView ( );
      m_gv->setScene ( new QGraphicsScene() );
      outerLayout->insertWidget ( 0, m_gv, 1 );
      }

      void QGuiMainScreen::show()
      {
      QMainWindow::show();

      // m_gv->setViewport ( new QGLWidget() );

      qint32 sceneWidth = m_gv->viewport()->width();
      qint32 sceneHeight = m_gv->viewport()->height();
      qDebug() << "Viewport: "<< sceneWidth << sceneHeight;
      qDebug() << "GraphicsView: " << m_gv->width() << m_gv->height();
      qDebug() << "CentralWidget: " << this->centralWidget()>width() << this>centralWidget()->height();
      qDebug() << "MainWindow: " << this->width() << this->height();

      m_gv->scene()->setSceneRect ( -sceneWidth / 2, -sceneHeight / 2, sceneWidth, sceneHeight );
      }

      int main ( int argc, char *argv[] )
      {
      QApplication app ( argc, argv );

      QGuiMainScreen myWnd;
      myWnd.show();

      return app.exec();
      }

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            bjnilsen Bjørn Erik Nilsen
            janichol Andy Nichols
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes