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

QHBoxLayout miscalculates size needed for QPushButton-derived subclasses

    XMLWordPrintable

Details

    • macOS

    Description

      In the example below, the widgets in the QHBoxLayout are drawn overlapping one another. Note that if you change the derivation of MyButton from QPushButton to QAbstractButton, the problem goes away.

      main.cpp
      #include <QtGui>
      
      class MyButton : public QPushButton {
      public:
        explicit MyButton(Qt::GlobalColor color, QWidget *parent = NULL)
          : QPushButton(parent), color_(color) {
          setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        }
        QSize sizeHint() const {
          return QSize(50, 25);
        }
      protected:
        void paintEvent(QPaintEvent *) {
          QPainter painter(this);
          painter.setOpacity(0.5);
          painter.fillRect(0, 0, width(), height(), color_);
        }
      private:
        Qt::GlobalColor color_;
      };
      
      int main(int argc, char **argv) {
        QApplication app(argc, argv);
        QWidget widget;
        QHBoxLayout *layout = new QHBoxLayout;
        layout->setSpacing(0);
        MyButton *w1 = new MyButton(Qt::red);
        MyButton *w2 = new MyButton(Qt::green);
        MyButton *w3 = new MyButton(Qt::blue);
        layout->addWidget(w1);
        layout->addWidget(w2);
        layout->addWidget(w3);
        widget.setLayout(layout);
        widget.show();
        return app.exec();
      }
      

      See http://stackoverflow.com/questions/3978889/why-is-qhboxlayout-causing-widgets-to-overlap for screen shot.

      Attachments

        Issue Links

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

          Activity

            People

              smd Jan Arve
              dmateer Dave Mateer
              Votes:
              8 Vote for this issue
              Watchers:
              18 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes