Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Word Wrapped QLabel提前切断文本_Python 3.x_Pyqt5 - Fatal编程技术网

Python 3.x Word Wrapped QLabel提前切断文本

Python 3.x Word Wrapped QLabel提前切断文本,python-3.x,pyqt5,Python 3.x,Pyqt5,我基本上只是想在布局的顶部添加一个标题来进行简要的描述,但是Qt已经开始妨碍了我 我启用了自动换行,并且QLabel将根据内容的长度动态增加其高度。。。但总是太短了 我的代码: class Window(QMainWindow): def __init__(self, qtpy): super().__init__() self.qtpy = qtpy self.setMinimumSize(250, 150) self

我基本上只是想在布局的顶部添加一个标题来进行简要的描述,但是Qt已经开始妨碍了我

我启用了自动换行,并且
QLabel
将根据内容的长度动态增加其高度。。。但总是太短了

我的代码:

class Window(QMainWindow):

    def __init__(self, qtpy):
        super().__init__()
        self.qtpy = qtpy
        self.setMinimumSize(250, 150)
        self.resize(600, 450)

class ThemeTool(Window):

    def __init__(self, qtpy):
        super().__init__(qtpy)
        self.setWindowTitle("qtpy-rant Theme Tool")

        widget = QWidget()
        layout = QBoxLayout(QBoxLayout.LeftToRight)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        widget.setLayout(layout)
        self.setCentralWidget(widget)

        sidebar = QWidget()
        sidebar.setMaximumWidth(150)
        sidebar_layout = QBoxLayout(QBoxLayout.TopToBottom)
        sidebar_layout.setAlignment(Qt.AlignTop)
        sidebar.setLayout(sidebar_layout)
        layout.addWidget(sidebar)

        palette = sidebar.palette()
        sidebar.setAutoFillBackground(True)
        palette.setColor(sidebar.backgroundRole(), QColor("#e3e3e3"))
        sidebar.setPalette(palette)

        sidebar_label = QLabel()
        sidebar_label.setWordWrap(True)
        sidebar_label.setText("The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors The Theme Tool is used to preview a theme and detect errors")
        sidebar_label.adjustSize()
        sidebar_layout.addWidget(sidebar_label)

        palette = sidebar_label.palette()
        sidebar_label.setAutoFillBackground(True)
        palette.setColor(sidebar_label.backgroundRole(), QColor("#ff0000"))
        sidebar_label.setPalette(palette)

        theme_area = QWidget()
        theme_area_layout = QBoxLayout(QBoxLayout.TopToBottom)
        theme_area.setLayout(theme_area_layout)
        layout.addWidget(theme_area, 1)

qapplication = QApplication(sys.argv)
main_window = ThemeTool(None)
main_window.show()
qapplication.exec_()

我想补充一些关于这个具体问题的见解

这种行为有多种原因,其中最重要的是使用具有富文本的子窗口小部件时的布局问题(为了澄清,即使您没有使用富文本,在QLabel上启用换行将使其内容在内部富文本)。下文对此进行了解释:

在标签小部件中使用富文本会给其父小部件的布局带来一些问题。出现问题的原因是当标签为word包装时,Qt的布局管理器处理富文本的方式

考虑到富文本内容的大小提示的计算相当复杂,使用QLabels,它通常会导致布局和标签之间的多次递归调用。虽然所有这些对用户来说都是非常即时和透明的,但这有时容易出现意外行为,其结果是QLabel在某些条件下无法正确计算其大小

请注意,在正常情况下,将添加标签并在标签上设置对齐方式,而不是在布局上

在进一步解释之前,需要考虑的一个重要因素是:Qt下的布局管理使用一个名为的抽象项,它可以作为任何可以由布局管理的内容的“参考”。当您将一个小部件添加到布局中时,Qt实际上为该小部件创建了一个新的QLayoutItem,并且正是该项添加到布局中;添加布局时也会发生同样的情况。但是,等等:所有Qt布局都是QLayout的子类,它实际上继承了QLayoutItem

当您使用简单的
addWidget(someWidget)
时,该小部件将尝试使用布局提供的整个“单元”,但当指定对齐方式(
addWidget(someWidget,alignment=Qt.AlignmentFlag)
)时,小部件将仅扩展到其大小提示

在您的情况下,问题是由于您已经在布局“项”(垂直框布局)上设置了对齐方式,这与在向布局添加项时指定对齐方式相同,如上所述。在这种情况下,布局[item]将使用其大小提示,但由于富文本的上述问题,这并不很好。一般来说,您几乎不应该设置布局的对齐方式

通常,只需将标签添加到布局中并指定标签上的对齐方式,但这显然不适用于您的情况:标签将正确对齐顶部的文本,但仍将尝试占据整个可用单元格空间,因为你也在设置背景,你会看到整个可用空间的颜色

虽然您可以在添加标签后使用
addStretch()
,但每当您需要添加其他小部件时,这可能是一个小问题:新小部件将在拉伸后添加,在标签和新小部件之间留下空白。在这种情况下,唯一的解决方案是始终使用insertWidget和基于项目计数的索引:

        sidebar_layout.insertWidget(sidebar_layout.count() - 1, newWidget)
这是可行的,但它不是很舒服,并且使代码可读性降低

最简单的解决方案是在添加标签而不是在布局上指定对齐方式:

        # remove the following
        # sidebar_layout.setAlignment(Qt.AlignTop)
        # ...
        sidebar_label = QLabel()
        sidebar_label.setWordWrap(True)
        sidebar_label.setText("")
        # this is also unnecessary
        # sidebar_label.adjustSize()
        sidebar_layout.addWidget(sidebar_label, alignment=Qt.AlignTop)
#删除以下内容
#侧栏布局设置对齐(Qt.AlignTop)
# ...
边栏标签=QLabel()
侧边栏标签setWordWrap(True)
侧边栏_label.setText(“”)
#这也是不必要的
#侧边栏_标签。调整大小()
sidebar_layout.addWidget(sidebar_标签,alignment=Qt.AlignTop)
最后,
adjustSize()
仅适用于未添加到布局或顶层小部件(windows)的小部件;因为您要将小部件添加到布局中,所以该调用是无用的。

另外,与使用
QBoxLayout(orientation)
构造函数不同,通常最好使用direct类和。我想补充一些关于这个特定问题的见解

这种行为有多种原因,其中最重要的是使用具有富文本的子窗口小部件时的布局问题(为了澄清,即使您没有使用富文本,在QLabel上启用换行将使其内容在内部富文本)。下文对此进行了解释:

在标签小部件中使用富文本会给其父小部件的布局带来一些问题。出现问题的原因是当标签为word包装时,Qt的布局管理器处理富文本的方式

考虑到富文本内容的大小提示的计算相当复杂,使用QLabels,它通常会导致布局和标签之间的多次递归调用。虽然所有这些对用户来说都是非常即时和透明的,但这有时容易出现意外行为,其结果是QLabel在某些条件下无法正确计算其大小

请注意,在正常情况下,将添加标签并在标签上设置对齐方式,而不是在布局上

在进一步解释之前,需要考虑的一个重要因素是:Qt下的布局管理使用一个名为的抽象项,它可以作为任何可以由布局管理的内容的“参考”。当您将一个小部件添加到布局中时,Qt实际上为该小部件创建了一个新的QLayoutItem,并且正是该项添加到布局中;添加布局时也会发生同样的情况。但是,等等:所有Qt布局都是QLayout的子类,它实际上继承了QLayoutItem

当您使用简单的
addWidget(someWidget)
时,该小部件将尝试使用布局提供的整个“单元”,但当指定对齐方式时(
addWidget(someWidget))