EDJOE
EDJOE

Reputation: 5

How in PyQt remove extra space between two qlabels in layout?

I have a QHBoxLayout and two labels in it. One is a picture, another - just text. How to make them closer to each other?

Here are examples:

enter image description here

Code:

from PyQt4 import QtGui, QtCore
import sys

app = QtGui.QApplication(sys.argv)

window = QtGui.QWidget()


vlay = QtGui.QVBoxLayout()
hlay = QtGui.QHBoxLayout()

vlay.addLayout(hlay)
window.setLayout(vlay)

label_pic = QtGui.QLabel()
image = QtGui.QPixmap()
image.load('123.jpg')
label_pic.setPixmap(image)

hlay.addWidget(label_pic)

label_text = QtGui.QLabel('Any text')
hlay.addWidget(label_text)

window.show()
sys.exit(app.exec_())

One more picture

Upvotes: 1

Views: 1628

Answers (1)

G.M.
G.M.

Reputation: 12879

From the images it's not immediately clear where one QLabel ends and the other starts but... I think you have a couple of obvious options.

Firstly, you could add a stretch to the rightmost QLabel...

hlay.addWidget(label_text, 1) # Note the `1' stretch factor.

Or, secondly, you could explicitly add a stretch item...

hlay.addWidget(label_text)
hlay.addStretch(1)

Upvotes: 2

Related Questions