python - PyQt:让小部件在 QDialog 中自动调整大小

标签 python qt pyqt qdialog

当对话框本身调整大小时,我很难让 QDialog 中的小部件自动调整大小。

在下面的程序中,如果您调整主窗口的大小,文本区域会自动调整大小。但是,当调整对话框大小时,对话框中的文本区域保持相同大小。

有没有办法让对话框中的文本区域自动调整大小?我试过在对话框本身和其中的两个小部件上使用 setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored),但这似乎没有效果。

如果相关的话,我在 openSuSE 10.2 上使用 Qt 3.3.7 版和 PyQt 3.5.5-29 版。

import sys
from qt import *

# The numbers 1 to 1000 as a string.
NUMBERS = ("%d " * 1000) % (tuple(range(1,1001)))

# Add a textarea containing the numbers 1 to 1000 to the given
# QWidget.
def addTextArea(parent, size):
    textbox = QTextEdit(parent)
    textbox.setReadOnly(True)
    textbox.setMinimumSize(QSize(size, size*0.75))
    textbox.setText(NUMBERS)


class TestDialog(QDialog):
    def __init__(self,parent=None):
        QDialog.__init__(self,parent)
        self.setCaption("Dialog")
        everything = QVBox(self)

        addTextArea(everything, 400)
        everything.resize(everything.sizeHint())


class TestMainWindow(QMainWindow):
    def __init__(self,parent=None):
        QMainWindow.__init__(self,parent)
        self.setCaption("Main Window")
        everything = QVBox(self)

        addTextArea(everything, 800)

        button = QPushButton("Open dialog", everything)
        self.connect(button, SIGNAL('clicked()'), self.openDialog)        

        self.setCentralWidget(everything)
        self.resize(self.sizeHint())

        self.dialog = TestDialog(self)

    def openDialog(self):
        self.dialog.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainwin = TestMainWindow(None)
    app.setMainWidget(mainwin)
    mainwin.show()
    app.exec_loop()

最佳答案

QMainWindow 具有 QDialog 所没有的中央小部件的特殊行为。要实现所需的行为,您需要创建一个 layout , 将文本区域添加到布局并将布局分配给对话框。

关于python - PyQt:让小部件在 QDialog 中自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/406939/

相关文章:

python - 如何将 .ui 文件转换为 .py 文件

Python + pyQT - 将参数发送到连接的 Signal 函数

python - Django:如何在 gereric DetailView 中使用自定义 slug(通用详细信息 View DetailView 必须使用对象 pk 或 slug 调用)

python - 检查python中每行的运行时间

qt - 当用户打开菜单或调整窗口大小时,QMainWindow 停止接收 QEvent::UpdateRequest

python - 副窗无槽

Python确保对象出现在列表的末尾

python - 是否可以在 Flask-WTForms 中设置标签的一部分样式?

qt - 如何实现基于树的 QComboBox

Qt 在另一个小部件中添加一个小部件?