Johnnylin
Johnnylin

Reputation: 535

How to delete the space between QLabel and QLineEdit

I have a QDialog window like this and I want to delete the space between 'Length', 'n', 'm' and corresponding QLineEdit input boxes. How can I achieve that ?

demo

Upvotes: 2

Views: 2436

Answers (3)

ymoreau
ymoreau

Reputation: 4036

You have to decide what to do with the space you want to remove :

  • Reduce the whole widget : it depends on the surrounding layout, adding an horizontal layout on right or left of it will put the space out of your widget.
  • Space on the left of your labels : you can align the label text to the right as ekhumoro suggested, or directly reduce and align to the right the whole frame by adding an horizontal spacer on its left (in the surrounding layout).
  • Space on the right of your line-edits : like above you can add horizontal spacers or reduce and align the frame.
  • Expand the line-edits : remove their fixed width (default horizontal policy is expanding) or set a bigger one.

The point is : the space has to be somewhere except if you reduce the parent widget or enlarge some the inner widgets. Size-policy is useful to tell which widget should take the available space, spacers are useful to let empty space between widgets.

Upvotes: 0

ekhumoro
ekhumoro

Reputation: 120768

All you need to do is reset the alignment of the labels:

    label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlighVCenter)

This can also be done in via the Property Editor in Qt Designer.

Upvotes: 1

m. c.
m. c.

Reputation: 895

If you use gridlayout, I am not sure why your output looks like that. Generally Qt will not leave huge empty space like that, There are three possibility I can think of:

  1. You have many SPACE after Length:, M: or N:
  2. The layoutHorizontalSpacing is too large in your grid manager.
  3. The layoutColumnStretch was set to in favor of label in your grid manager, should be "0,0", not "1,0". I mean, stretch of Label should not be higher than lineedit.

Still, I would use a simple form layout in your application.

Upvotes: 1

Related Questions