Align QPushButton and QLineEdit



  • Hello,

    QPushButton and QLineEdit have the same apparent height. But when I put both of them into a HBoxLayout it grows larger than any of the items. Upon closer inspection it seems that QPushButton has some kind of padding around it, please see the attached screenshot:

    "http://imgur.com/DrF75j8":http://imgur.com/DrF75j8

    How to remove the padding so that the layout has minimum encompassing height (so that it touches QLineEdit)?



  • you should call QHBoxLayout(instance).setContentsMargins(0,0,0,0);



  • Sorry, should have mentioned it - yes, I set contents margins to (0, 0, 0, 0) already.



  • Could you post your code please?



  • Sure:

    @

    #include "mainwindow.h"
    #include <QtWidgets>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    QWidget *widget = new QWidget(this);
    QHBoxLayout *layout = new QHBoxLayout(this);
    QLineEdit *line = new QLineEdit();
    QPushButton *button = new QPushButton("Push Button");
    widget->setLayout(layout);
    widget->setFixedWidth(200);
    layout->addWidget(line);
    layout->addWidget(button);
    layout->setContentsMargins(0, 0, 0, 0);
    }

    MainWindow::~MainWindow()
    {

    }

    @

    So my question is, why is the QLineEdit and QPushButton not touching the upper frame?



  • Try...
    @
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    QWidget *widget = new QWidget(this);
    QHBoxLayout *layout = new QHBoxLayout(widget);
    QLineEdit *line = new QLineEdit(widget);
    QPushButton *button = new QPushButton("Push Button");
    line->setFixedHeight(button->sizeHint().height());
    widget->setFixedWidth(200);
    layout->addWidget(line);
    layout->addWidget(button);
    layout->setContentsMargins(0, 0, 0, 0);
    this->setCentralWidget(widget);
    }
    @
    What I am doing is making the QLineEdit's height the same size of that of the QPushButton's.
    Also since MainWindow already has a Layout associated with it, I have assigned widget as the parent of the lay out.
    Hope that helps.



  • Thanks but this seems to grow the QLineEdit height whereas I would like to shrink QPushButton height.

    Perhaps the word "align" is a bit misleading. What I am after is how to remove the invisible padding around QPushButton so that it does not grow the layout beyond QLineEdit's height.



  • Sorry I took time to understand what you really wanted.
    Well...
    Stylesheets to the rescue here!

    @
    button->setStyleSheet("QPushButton {"
    "border-width: 2px;"
    "padding: 1px;"
    "}");
    @



  • Thank you for the reply. Unfortunately the style sheet distorts the whole GUI (button gets square-shaped and the widgets misalign). But! It seems it is a Mac-only issue as the very first try from above works under Qt 4.8 on Ubuntu. I filed a bug report, thanks once more for the help!


Log in to reply
 

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