Qt:HBoxLayout-停止MainWindow调整到内容的大小 [英] Qt: HBoxLayout - stop MainWindow from resizing to contents
问题描述
似乎大多数人都在问如何调整其QMainWindow
的大小以适应其内容-我有相反的问题,我的MainWindow 可以调整大小,我不知道为什么.
It seems most people are asking how to make their QMainWindow
resize to its contents - I have the opposite problem, my MainWindow does resize and I don't know why.
当我将QLabel
设置为更长的文本时,我的主窗口突然变大了,我不知道为什么会这样.
When I set my QLabel
to a longer text, my mainwindow suddenly gets bigger, and I can't find out why that happens.
以下示例代码基本上包含:
The following example code basically contains:
- A
QMainWindow
- A
QWidget
作为中央窗口小部件- A
QVBoxLayout
作为其子代- 里面有一个
LabelBar
.
- A
QMainWindow
- A
QWidget
as central widget- A
QVBoxLayout
as a child of that- A
LabelBar
inside that.
LabelBar
是QWidget
,它依次包含:- A
QHBoxLayout
- 其中有两个
QLabel
.
- A
QHBoxLayout
- Two
QLabel
s in that.
一秒钟后,
QTimer
将标签设置为较长的文本以演示问题.After a second, a
QTimer
sets the label to a longer text to demonstrate the issue.PyQt示例代码:
from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QLabel, QWidget, QMainWindow, QVBoxLayout, QSizePolicy) from PyQt5.QtCore import QTimer class MainWindow(QMainWindow): def __init__(self): super().__init__() cwidget = QWidget(self) self.setCentralWidget(cwidget) self.resize(100, 100) vbox = QVBoxLayout(cwidget) vbox.addWidget(QWidget()) self.bar = LabelBar(self) vbox.addWidget(self.bar) timer = QTimer(self) timer.timeout.connect(lambda: self.bar.lbl2.setText("a" * 100)) timer.start(1000) class LabelBar(QWidget): def __init__(self, parent=None): super().__init__(parent) hbox = QHBoxLayout(self) self.lbl1 = QLabel(text="eggs") hbox.addWidget(self.lbl1) self.lbl2 = QLabel(text="spam") hbox.addWidget(self.lbl2) if __name__ == '__main__': app = QApplication([]) main = MainWindow() main.show() app.exec_()
推荐答案
主窗口不断扩大,因为这是使用布局的目标.布局对小部件的大小有要求,以确保正确显示所有内容.要求取决于子窗口小部件.例如,默认情况下
QLabel
水平增长,并且需要空间以适应其内容.有很多方法可以防止窗口增大,并且产生的行为也各不相同:Main window grows because it's the goal of using layouts. Layouts make size requirements for their widgets to ensure that all content is displayed correctly. Requirements depend on child widgets. For example,
QLabel
by default grows horizontally and require space to fit its content. There are many ways to prevent window growing, and the resulting behavior varies:- 您可以将
QLabel
放在QScrollArea
中.标签的文本太长时,将显示滚动条. - 您可以使用
self.lbl2.setWordWrap(True)
启用自动换行.只要您在文本上设置一些空格,QLabel
就会将其显示在多行中,并且窗口会稍微向垂直方向增长,而不是向水平方向增长. - 您可以使用
self.lbl2.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed)
忽略QLabel
的尺寸提示.QLabel
的内容不会影响其布局或父窗口小部件的大小.太大的文本将被截断.
- You can put
QLabel
in aQScrollArea
. When label's text is too long, scrollbars will appear. - You can enable word wrap using
self.lbl2.setWordWrap(True)
. As long as you set text with some spaces,QLabel
will display it in several lines, and window will grow vertically a bit instead of growing horizontally. - You can ignore
QLabel
's size hint usingself.lbl2.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed)
.QLabel
's content will not affect its layout or parent widget size. Too large text will be truncated.
这篇关于Qt:HBoxLayout-停止MainWindow调整到内容的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Two
- 其中有两个
- A
- A
- A
- 里面有一个
- A
- A