PyQt:让小部件在 QDialog 中自动调整大小 [英] PyQt: getting widgets to resize automatically in a QDialog

查看:86
本文介绍了PyQt:让小部件在 QDialog 中自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having difficulty getting widgets in a QDialog resized automatically when the dialog itself is resized.

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

In the following program, the textarea resizes automatically if you resize the main window. However, the textarea within the dialog stays the same size when the dialog is resized.

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

Is there any way of making the textarea in the dialog resize automatically? I've tried using setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) on the dialog itself and the two widgets within, but that seems to have no effect.

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

I'm using Qt version 3.3.7 and PyQt version 3.5.5-29 on openSuSE 10.2, if that's relevant.

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 没有的特殊行为.要实现所需的行为,您需要创建一个布局,将文本区域添加到布局中并将布局分配给对话框.

QMainWindow has special behavior for the central widget that a QDialog does not. To achieve the desired behavior you need to create a layout, add the text area to the layout and assign the layout to the dialog.

这篇关于PyQt:让小部件在 QDialog 中自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆