OSX: wrong button layout



  • I have searched the web about this problem and this seems to be an unsolved problem.

    I am creating a window with several buttons with a layout in rows and columns, some buttons spawning multiple columns. Here is a simple version of this problem using 2 buttons in the top row, and one in the bottom row.

    mac-x86:buttons steffen$ cat buttons.pro
    TARGET = buttons

    CONFIG += qt
    QT += widgets

    SOURCES = buttons.cc

    mac-x86:buttons steffen$ cat buttons.cc
    #include <QApplication>
    #include <QBoxLayout>
    #include <QGridLayout>
    #include <QMainWindow>
    #include <QPushButton>

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

    for (int i = 0; i < 3; ++i) {
        QMainWindow *win = new QMainWindow();
        QWidget *root = new QWidget(win);
        win->setCentralWidget(root);
        QPushButton *left = new QPushButton("Left", root);
        QPushButton *right = new QPushButton("Right", root);
        QPushButton *full = new QPushButton("Full", root);
    
        switch (i) {
            case 0:
            case 2: {
                // Use a grid layout.
                QGridLayout *main = new QGridLayout(root);
                main->addWidget(left, 0, 0, 1, 1);
                main->addWidget(right, 0, 1, 1, 1);
                main->addWidget(full, 1, 0, 1, 2);
                if (i == 2) {
                    // Try the layout-uses-widget-rect setting.
                    left->setAttribute(Qt::WA_LayoutUsesWidgetRect);
                    right->setAttribute(Qt::WA_LayoutUsesWidgetRect);
                    full->setAttribute(Qt::WA_LayoutUsesWidgetRect);
                    win->setWindowTitle("Grid layout using widget rect setting");
                } else {
                    win->setWindowTitle("Grid layout");
                }
                break;
            }
    
            default: {
                // Use box-layouts.
                QVBoxLayout *main = new QVBoxLayout(root);
                QHBoxLayout *hbox = new QHBoxLayout();
                main->addLayout(hbox);
                hbox->addWidget(left);
                hbox->addWidget(right);
                main->addWidget(full);
                win->setWindowTitle("Box layouts");
                break;
             }
        }
        win->show();
    }
    
    app.exec();
    

    }

    This creates 3 windows where I try different layouts and options. On Linux, all three variations create an identical layout (as it should):

    0_1505714093725_b868be9e-e640-4e56-9096-43ed75c0bc69-image.png

    On OSX with Qt 5.9.1 though, only the GridLayout creates the correct layout:

    0_1505714149259_e018fa22-10d2-484b-9429-508d632a89e7-image.png

    When using the BoxLayout-version the "Left" and "Right" buttons have too much space around them and are not vertically aligned with the "Full" button.

    Others have suggested to use the Qt::WA_LayoutUsesWidgetRect settings, but this only makes things worse. While it fixes the alignment problem, there is even more space around all buttons.

    Some will probably say, why not just using the first version with the GridLayout? The problem is that it is not always possible to put everything into a GridLayout, and it makes the software design unnecessary difficult.

    Has anyone found a way around this?




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.