Search code examples
qtalignmentqlayoutqgridlayout

Different alignment of widgets in QGridLayout


The following code (Qt 5, same behaviour with Qt 4.8) uses a QGridLayout to add widgets above and to the left of a QScrollArea (to serve as a kind of header later):

#include <QApplication>
#include <QMainWindow>
#include <QGridLayout>
#include <QScrollArea>

class ColoredWidget : public QWidget {
public:
    ColoredWidget(const  QColor& color, QWidget* parent) : QWidget(parent) {
        QPalette pal;
        QBrush brush(color);
        brush.setStyle(Qt::SolidPattern);
        pal.setBrush(QPalette::Active, QPalette::Window, brush);
        setPalette(pal);
        setAutoFillBackground(true);
    }
};

class MainWindow : public QMainWindow {
public:
    MainWindow(QWidget* parent) : QMainWindow(parent) {
        resize(300, 400);

        QWidget* centralwidget = new ColoredWidget(QColor(0xff, 0xf0, 0xb5), this);

        QGridLayout* layout = new QGridLayout();
        centralwidget->setLayout(layout);
        setCentralWidget(centralwidget);

        // create widget with fixed height of 20 px and maximum width of 200
        QWidget* topHeader = new ColoredWidget(Qt::green, centralwidget);
        topHeader->setMaximumWidth(200);
        topHeader->setFixedHeight(20);

        // create widget with fixed width of 20 px and maximum height of 200
        QWidget* leftHeader = new ColoredWidget(Qt::blue, centralwidget);
        leftHeader->setFixedWidth(20);
        leftHeader->setMaximumHeight(200);

        // create scroll area as main widget
        QWidget* view = new QScrollArea();

        layout->addWidget(topHeader,  0, 1);
        layout->addWidget(leftHeader, 1, 0);
        // layout->addWidget(leftHeader, 1, 0, Qt::AlignTop); // widget not displayed at all!
        layout->addWidget(view, 1, 1);
    }
};


int main(int argc, char ** argv) {
    QApplication app( argc, argv );
    MainWindow win(0);
    win.show();
    return app.exec();
}

The QGridLayout documentation says that "Columns and rows behave identically", thus I would expect that the two widgets are layouted the same way.

However, the left one is automatically vertically centered, while the top widget is aligned to the left within the cell (and not centered horizontally). I would like to have the left widget also at a fixed position, means aligned to the top:

Strange Layout

  • Which property causes this different behaviour between the two widgets, one beeing centered, the other being at a fixed position?
  • How can I influence the alignment (I can probably add a flexible spacer to fix it, but ideally I would like to avoid that)? I tried with Qt::AlignTop, but that made the blue widget disappear.

Solution

  • Although not a minimal solution you could probably fix the alignments by adding a QHBoxLayout in the cell for the green widget and a QVBoxLayout in the cell for the blue widget. Add the green widget to the QHBoxLayout and the blue widget to the QVBoxLayout. Then apply addStretch(n) to both the QHBoxLayout and QVBoxLayout. Maybe this is what you already mentioned you could do and then we are at the same level of knowledge concerning this. I believe this is the way I have solved these kind of issues and I haven't spent more time on it.