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

Issue

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()
scroll.setWidgetResizable(True)

grid = QGridLayout()
inner = QFrame(scroll)
inner.setLayout(grid)

scroll.setWidget(inner)

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

grid.setVerticalSpacing(0)

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)


scroll.show()
app.exec_()

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 😀

Solution

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.setWordWrap(True)
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 *