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.

4

1 回答 1

15

解决方案 1:在代码中

我发现如果我改变(在 Ui_Dialog 中),我可以让它“工作”:

self.scrollArea.setWidget(self.scrollAreaWidgetContents)

self.scrollArea.setWidget(self.label)

我不知道为什么 QtDesigner 会创建不起作用的代码,而且我不完全理解为什么代码不起作用,当这似乎是。self.label 有 self.scrollAreaWidgetContents 作为它的父级,所以连接似乎在那里。

因此,我猜测我在 QtDesigner 中做错了什么,这阻止了它导出有效的代码。

解决方案 2:在 QT 设计器中

好的,所以我发现如果我执行以下操作,我可以通过 QtDesigner 使其工作:

(1)创建一个ScrollArea【此时无法设置ScrollArea的布局

(2)在ScrollArea里面放一个Label

(3) 右键单击​​ScrollArea,设置其布局(如Horizo​​ntal Layout)

(4) 出口

这似乎工作得很好。但是,如果我执行以下操作,它将不起作用:

(1) 创建滚动区域

(2)在ScrollArea中放置一个Layout(如Horizo​​ntal Layout)

(3) 在布局内放置一个标签

(4) 出口

我不确定这里发生了什么,但是,尽管如此,这解决问题的两种方法。

于 2013-06-30T18:07:45.777 回答