PyQt 4: creating a scrollable label

I am trying to create a label where I can record the current status of the program - for example:

"Reading data ..."

"Data processing..."

"Complete".

If the text reaches the bottom of the label, it should automatically scroll with the text to make sure that it displays the last message (for example, the console window). I’ve been scrolling through shortcuts and scrolling for more than an hour, but to no avail. I tried putting my shortcut inside scrollarea (which seems to offer related answers here) - this code is generated from Qt Designer:

class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName(_fromUtf8("Dialog")) Dialog.resize(218, 137) self.frame = QtGui.QFrame(Dialog) self.frame.setGeometry(QtCore.QRect(209, 399, 161, 111)) self.frame.setFrameShape(QtGui.QFrame.StyledPanel) self.frame.setFrameShadow(QtGui.QFrame.Raised) self.frame.setObjectName(_fromUtf8("frame")) self.scrollArea = QtGui.QScrollArea(Dialog) self.scrollArea.setGeometry(QtCore.QRect(10, 10, 201, 121)) self.scrollArea.setWidgetResizable(True) self.scrollArea.setObjectName(_fromUtf8("scrollArea")) self.scrollAreaWidgetContents = QtGui.QWidget() self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 199, 119)) self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents")) self.label = QtGui.QLabel(self.scrollAreaWidgetContents) self.label.setGeometry(QtCore.QRect(15, 10, 151, 101)) self.label.setWordWrap(True) self.label.setObjectName(_fromUtf8("label")) self.scrollArea.setWidget(self.scrollAreaWidgetContents) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) 

My main .pyw file then contains:

 import sys from gui_test import * class MyForm(QtGui.QDialog): def __init__(self,parent=None): QtGui.QWidget.__init__(self,parent) self.ui = Ui_Dialog() self.ui.setupUi(self) self.ui.label.setText("Warning: When passing a QString to the constructor or calling setText(), make sure to sanitize your input, as QLabel tries to guess whether it displays the text as plain text or as rich text, a subset of HTML 4 markup. You may want to call setTextFormat() explicitly, eg in case you expect the text to be in plain format but cannot control the text source (for instance when displaying data loaded from the Web).") if __name__ == "__main__": app = QtGui.QApplication(sys.argv) myapp = MyForm() myapp.show() sys.exit(app.exec_()) 

And scrollarea just doesn't offer any scrollbars, although not all text is displayed. If I make the scrollbars always be turned on, they are grayed out, indicating that the text is not scrolling.

If anyone could help me, I would really appreciate it.

+4
source share
1 answer

SOLUTION 1: IN THE CODE

I found that I can make it work if I change (in Ui_Dialog):

 self.scrollArea.setWidget(self.scrollAreaWidgetContents) 

to

 self.scrollArea.setWidget(self.label) 

I have no idea why QtDesigner created code that does not work, and I do not quite understand WHY this code does not work when it seems. self.label has self.scrollAreaWidgetContents as its parent, so the connections seem to exist.

Accordingly, I assume that I am doing something wrong in QtDesigner, which prevents this from exporting code that works.

SOLUTION 2: IN QT DESIGNER

Ok, so I found that I can get it to work through QtDesigner if I do the following:

(1) Create ScrollArea [cannot set ScrollArea layout at this point]

(2) Put a label inside ScrollArea

(3) Right-click on ScrollArea, set its layout (e.g. horizontal layout)

(4) Export

Everything seems to be working fine. HOWEVER, it DOES NOT work if I do the following:

(1) Create ScrollArea

(2) Put the layout in ScrollArea (e.g. horizontal layout)

(3) Place a label inside the layout

(4) Export

I'm not quite sure what is happening here, but, nevertheless, these are two solutions to the problem.

+8
source

Source: https://habr.com/ru/post/1486544/


All Articles