Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Align QPushButton and QLineEdit
Qt 6.11 is out! See what's new in the release blog

Align QPushButton and QLineEdit

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 12.8k Views 1 Watching
  • 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 Offline
    E Offline
    ecir.hana
    wrote on last edited by
    #1

    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
    0
    • P Offline
      P Offline
      prady_80
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ecir.hana
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          prady_80
          wrote on last edited by
          #4

          Could you post your code please?

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ecir.hana
            wrote on last edited by
            #5

            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
            0
            • P Offline
              P Offline
              prady_80
              wrote on last edited by
              #6

              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
              0
              • E Offline
                E Offline
                ecir.hana
                wrote on last edited by
                #7

                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
                0
                • P Offline
                  P Offline
                  prady_80
                  wrote on last edited by
                  #8

                  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
                  0
                  • E Offline
                    E Offline
                    ecir.hana
                    wrote on last edited by
                    #9

                    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
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved