Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Align QPushButton and QLineEdit

    General and Desktop
    2
    9
    10613
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • E
      ecir.hana last edited by

      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)?

      1 Reply Last reply Reply Quote 0
      • P
        prady_80 last edited by

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

        1 Reply Last reply Reply Quote 0
        • E
          ecir.hana last edited by

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

          1 Reply Last reply Reply Quote 0
          • P
            prady_80 last edited by

            Could you post your code please?

            1 Reply Last reply Reply Quote 0
            • E
              ecir.hana last edited by

              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?

              1 Reply Last reply Reply Quote 0
              • P
                prady_80 last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • E
                  ecir.hana last edited by

                  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.

                  1 Reply Last reply Reply Quote 0
                  • P
                    prady_80 last edited by

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

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

                    1 Reply Last reply Reply Quote 0
                    • E
                      ecir.hana last edited by

                      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!

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post