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

QGraphicsGridLayout misplaces rightmost widget if its column span is > 1

    XMLWordPrintable

Details

    • Bug
    • Resolution: Duplicate
    • P2: Important
    • Some future release
    • 4.6.3, 4.7.0
    • Widgets: GraphicsView
    • None
    • This happens in all environments.
    • 4f072e2d3d7e429359ff15a615d02712bff7ee51 1112b5ca9b448ce26cdd9eb7cc67abc7f9d537d8

    Description

      If the right-most widget you add to a grid layout has a column span > 1, it fails to be placed correctly. The expected behavior is that the span is ignored.

      The following code demonstrates the bug. The "Box" widget has a preferred size of (100, 100) and takes a colored background as a construction parameter. One Box is used as a "window" that has a grid layout assigned. We then add three Box items to the grid layout. The first two are "normal" boxes, colors red and green. The final, blue, Box item has a column span of 2.

      The example shows that the final widget isn't visible (see screenshot). The same code creates an expected result if you replace QGraphicsGridLayout with QGridLayout and use QWidget instead. It also behaves as expected if you specify a column span of 1 (or don't specify a column span).

      #include <QtGui>
      
      class Box : public QGraphicsWidget
      {
      public:
          Box(const QColor &color) : col(color)
          { }
          
          void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * = 0)
          {
              painter->fillRect(rect(), col);
          }
      
      protected:
          QSizeF sizeHint(Qt::SizeHint hint, const QSizeF &constraint = QSizeF(-1, -1)) const
          {
              return hint == Qt::PreferredSize
                  ? QSizeF(100, 100)
                  : QGraphicsWidget::sizeHint(hint, constraint); 
          }
      
      private:
          QColor col;
      };
      
      int main(int argc, char **argv)
      {
          QApplication app(argc, argv);
      
          Box *window = new Box(Qt::darkYellow);
          QGraphicsGridLayout *grid = new QGraphicsGridLayout;
          window->setLayout(grid);
      
          grid->addItem(new Box(Qt::red), 0, 0);
          grid->addItem(new Box(Qt::green), 0, 1);
      
          // grid->addItem(new Box(Qt::blue), 0, 2);
          grid->addItem(new Box(Qt::blue), 0, 2, 1, 2);
      
          QGraphicsScene scene;
          scene.addItem(window);
      
          QGraphicsView view(&scene);
          view.show();
      
          return app.exec();
      }
      

      Attachments

        1. grid-right.png
          grid-right.png
          8 kB
        2. grid-wrong.png
          grid-wrong.png
          8 kB
        3. main.cpp
          1 kB

        Issue Links

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

          Activity

            People

              smd Jan Arve
              bibr Andreas Aardal Hanssen
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes