PyQt 4: Making a label scrollable
Asked Answered
F

1

6

I am trying to create a label where I can write the current status of the program - e.g.:

"Reading data..."

"Processing data..."

"Complete."

If the text reaches the bottom of the label, then it should automatically scroll with the text, to ensure that it's showing the latest message (like a console window would do). I've been fiddling with labels and scrollareas for more than an hour now... and to no avail. I tried putting my label inside a scrollarea (which seems to be what the related answers on here suggest) - 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, e.g. 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 the scrollarea simply doesn't offer any scrollbars, even though not all the text is being displayed. If I force the scrollbars to always be on, they are greyed out, suggesting it doesn't think there's any text to scroll.

If anyone could help me out here, I'd really appreciate it.

Faustinafaustine answered 17/6, 2013 at 6:32 Comment(0)
F
17

SOLUTION 1: IN CODE

I discovered 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 would have created code that doesn't work, and I don't fully understand WHY that code didn't work, when this seems to. self.label had self.scrollAreaWidgetContents as its parent, so the connections seem to be there.

Accordingly, I'm guessing that I'm doing something wrong in QtDesigner that's preventing this from exporting code that works.

SOLUTION 2: IN QT DESIGNER

Okay, so I discovered that I can make it work through QtDesigner if I do the following:

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

(2) Place a Label inside the ScrollArea

(3) Right click on the ScrollArea, set its layout (e.g., Horizontal Layout)

(4) Export

This seems to work just fine. HOWEVER, it does NOT work if I do the following:

(1) Create a ScrollArea

(2) Place a Layout in the ScrollArea (e.g., Horizontal Layout)

(3) Place a Label inside the Layout

(4) Export

I'm not exactly sure what's going on here, but, nevertheless, these are two solutions to the problem.

Faustinafaustine answered 30/6, 2013 at 18:7 Comment(3)
Thank you for this hint, I was struggling with this as well.Hirohito
Very much helpfulOnce
Thank you. I had to implement both solutions to get it to work. Furthermore, I set Horizontal Policy and Vertical Policy to 'Fixed' for scrollArea, 'Preferred' for scrollAreaWidgetContents, and 'Preferred' for the label. Not sure if this matters...Flasher

© 2022 - 2024 — McMap. All rights reserved.