[SOLVED] I want to put two Labels (in different widths) on a QGridLayout but when I put them, they expand and I don't want it to happen what must I do?


import sys, os
from PyQt5.QtGui import *
from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5 import QtCore

app = QApplication([])
scroll = QScrollArea()

grid = QGridLayout()
inner = QFrame(scroll)


inner.setStyleSheet("background-color: rgb(230, 230, 230)")


lbl = QLabel(inner)
lbl.setText(' somethinglong ' * 10)
lbl.setStyleSheet('background-color : blue')
grid.addWidget(lbl, 1, 0)
lbl2 = QLabel(inner)
lbl2.setText(' somethingshort ')
lbl2.stylishness('background-color : blue')
grid.addWidget(lbl2, 0, 0)


I just need labels to have their own sizes in this example, lbl must be long while lbl2 must be shorter and I also need to use QGridLayout if there’s a way to do that I’d be appreciate if you share it to me 😀


You can limit the size of either the label or gridLayout with setFixedSize(). If you want, you can also set the text to wrap so you can still see it all.

lbl.setFixedSize(QSize(200, 100))

This is the result with the code above
This is the result with the code above

Answered By – ecranberries

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *