在QLabel上设置自动换行会打破窗口的大小限制

时间:2014-09-03 12:13:40

标签: python qt pyqt

我有以下代码:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " \
       "Nullam malesuada tellus in ex elementum, vitae rutrum enim vestibulum."

#==============================================================================
class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        # Widgets
        self.label = QLabel(TEXT, self) 
#         self.label.setWordWrap(True)    
        self.text = QTextEdit(self)
        self.text.setMinimumSize(480, 320)
        self.text.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # Layout
        self.layout = QGridLayout()
        self.layout.addWidget(self.label, 0, 0)
        self.layout.addWidget(self.text, 1, 0)
        self.layout.setContentsMargins(5, 5, 5, 5)
        self.layout.setSpacing(5)

        self.setLayout(self.layout)   

        self.adjustSize()
        self.show()           

#==============================================================================
if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    sys.exit(app.exec_())

它按预期工作,产生一个窗口,无法调整大小:

enter image description here

然而,当我取消注释self.label.setWordWrap(True)行时,这些约束似乎消失了。我可以将窗口调整为更小,完全打破布局,因为QTextEdit仍然保持其大小限制:

enter image description here

我尝试使用self.setMinimumSize(self.size())解决此问题。这适用于此特定示例,但如果大小较大则会中断,例如self.text.setMinimumSize(480, 800)。这使得窗口即使在创建时也太小,因此setMinimumSize无效:

enter image description here

有没有办法解决它并使窗口/布局仍然匹配QTextEdit的最小大小,即使启用自动换行也是如此?

版本信息:

OS: Windows-7-SP1 (32bit)
Python: 3.4.1
PyQt: 5.3.1
Qt: 5.3.1

2 个答案:

答案 0 :(得分:3)

正如@thuga在评论中建议的那样,问题在docs中已经提及reported,显然是“不会/不能解决”解决方案。

我找到了针对此特定问题here的解决方法的提示。它适用于我的示例以及我的应用程序: self.setMinimumSize(self.sizeHint())

然后代码如下:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " \
       "Nullam malesuada tellus in ex elementum, vitae rutrum enim vestibulum."

#==============================================================================
class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        # Widgets
        self.label = QLabel(TEXT, self) 
        self.label.setWordWrap(True)    
        self.text = QTextEdit(self)
        self.text.setMinimumSize(480, 800)
        self.text.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # Layout
        self.layout = QGridLayout()
        self.layout.addWidget(self.label, 0, 0)
        self.layout.addWidget(self.text, 1, 0)
        self.layout.setContentsMargins(5, 5, 5, 5)
        self.layout.setSpacing(5)

        self.setLayout(self.layout)   

        self.setMinimumSize(self.sizeHint())
        self.show()     

#==============================================================================
if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    sys.exit(app.exec_())

答案 1 :(得分:0)

我也有这个问题,QT 4.8,我无法升级我的QT,因为我正在使用仍然使用QT 4.8的QGIS 2.18插件。

我的工作是在调​​整大小时操纵maximumHeight(或者在你的情况下,你的QWebView)的QTextEdit。您需要为调整大小发出自定义信号,因为它在QT中不可用(请参阅:PyQt: Detect resizing in Widget-window resized signal

所以,我的代码是这样的:

class MetadataConverterDialog(QDialog):
    resized = pyqtSignal()
        # Adapted from https://stackoverflow.com/a/43126946/1198772
    def resizeEvent(self, event):
        """Emit custom signal when the window is re-sized.

        :param event: The re-sized event.
        :type event: QResizeEvent
        """
        self.resized.emit()
        return super(MetadataConverterDialog, self).resizeEvent(event)

    def after_resize(self):
        """Method after resizing the window."""
        max_height = self.height() - 275  # Magic number, to make it pretty
        self.metadata_preview_web_view.setMaximumHeight(max_height)

275是我需要备用以正确显示对话框的值(对于QWebView之前的小部件。您需要找到适合您对话框的值。