使用 PyQt4 调整对话框大小 [英] resizing a dialog with PyQt4

查看:70
本文介绍了使用 PyQt4 调整对话框大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码示例:

import sys
from PyQt4.QtGui import (QApplication, QHBoxLayout, QVBoxLayout, QDialog,
                                          QFrame, QPushButton, QComboBox)

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        moreButton = QPushButton('moreButton')
        moreButton.setCheckable(True)
        resizeButton = QPushButton('Resize')
        combo = QComboBox()
        combo.addItems(['item1', 'item2'])

        layout1 = QHBoxLayout()
        layout1.addWidget(moreButton)
        layout1.addWidget(resizeButton)

        layout2 = QHBoxLayout()
        layout2.addWidget(combo)
        self.frame = QFrame()
        self.frame.setLayout(layout2)
        self.frame.hide()

        layout3 = QVBoxLayout()
        layout3.addLayout(layout1)
        layout3.addWidget(self.frame)

        moreButton.toggled.connect(self.frame.setVisible)
        moreButton.clicked.connect(self.method)
        resizeButton.clicked.connect(self.method)

        self.setLayout(layout3)
        self.resize(630, 50)

    def method(self):
        if self.frame.isVisible():           
            self.resize(630, 150)
        else:
            self.resize(630, 250)

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

我运行它,当单击 moreButton 时,ComboBox 出现或消失.对话框的大小也会发生变化.但是如果我将方法更改为:

I run it and when moreButton clicked the ComboBox appears or disappears. Dialog's size also changes. But if I change the method to:

def method(self):
    if self.frame.isVisible():           
        self.resize(630, 150)
    else:
        self.resize(630, 50)

(为了在隐藏组合时设置初始大小)调整大小不起作用.但是,如果我单击 resizeButton - 它连接到相同的方法 - 调整大小工作正常.

(in order to set the initial size when combo is hidden) the resizing does not work. However, if I click resizeButton -which is connected to the same method- the resizing works properly.

我知道还有其他方法可以实现这样的结果(例如 layout.setSizeConstraint(QLayout.SetFixedSize)),但我想明确声明大小.

I know that there are other ways to achieve such a result (eg. layout.setSizeConstraint(QLayout.SetFixedSize)), but I want to declare size explicitly.

我做错了什么?

推荐答案

我的猜测是,您正在尝试调整 QDialog 的大小,以便在隐藏内容后重新调整其大小.所以在调用 resize 时,它有一个 minimumSize 来确保按钮和组合框可见.当您一段时间后调用它时,它现在具有适当的miminumSize并正确响应.

My guess is that you are trying to resize the QDialog before it has time to re-adjust its size after you hide stuff. So at the time resize is called it has a minimumSize that makes sure the buttons and the combobox visible. When you call it after some time, it now has proper miminumSize and responds properly.

快速修复是在调整大小之前手动覆盖 minimumSize:

A quick fix is manually overriding minimumSize before resizing:

def method(self):
    if self.frame.isVisible():
        # uncomment below, if you like symmetry :)
        # self.setMinimumSize(630, 150)
        self.resize(630, 150)
    else:
        self.setMinimumSize(630, 50)
        self.resize(630, 50)

但是,如果我要解决这个问题,我只会将管理调整大小留给布局并使用 sizeConstraint.无论如何,这就是这些布局的用途.

But, if I were to tackle this, I'd just leave managing resizing to the layout and use sizeConstraint. That's what these layouts for anyways.

这篇关于使用 PyQt4 调整对话框大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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