自动调整Widget高度
Automatically ajust QWidget height
我想知道如何创建一个小部件,当我编辑包含此小部件的 QDialog 的高度时,它会自动适应 space。
在下面的例子中,QPushButton 将适合 QDialog 的宽度,但不适合高度。但是,如果我创建 QTextEdit 而不是 QPushButton,QTextEdit 将完全适合 QDialog。
from PySide.QtGui import *
class c (QDialog):
def __init__(self):
QDialog.__init__(self)
self.setLayout(QVBoxLayout())
self.layout().setSpacing (0)
self.layout().setContentsMargins(0,0,0,0);
btn = QPushButton()
self.layout().addWidget(btn)
self.exec_()
a = c()
您需要设置尺寸策略才能展开。
我不知道 python 中的语法,但你可以轻松地从 C++ 翻译过来。
QPushButton* button = new QPushButton(this);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
The C++ documentation of the QBoxLayout Class 表示
QBoxLayout::addWidget(widget, stretch=0, alignment=0)
If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.
所以看看 QPushButton
的默认值 Policy
>>> btn = QPushButton()
>>> btn.sizePolicy().horizontalPolicy()
1
>>> btn.sizePolicy().verticalPolicy()
0
我们会发现,垂直策略是固定的。添加像
这样的按钮
[...]
btn = QPushButton()
policy = btn.sizePolicy().horizontalPolicy()
btn.setSizePolicy(policy, policy)
self.layout().addWidget(btn)
[...]
现在将通过您的对话框自动调整按钮。
我想知道如何创建一个小部件,当我编辑包含此小部件的 QDialog 的高度时,它会自动适应 space。
在下面的例子中,QPushButton 将适合 QDialog 的宽度,但不适合高度。但是,如果我创建 QTextEdit 而不是 QPushButton,QTextEdit 将完全适合 QDialog。
from PySide.QtGui import *
class c (QDialog):
def __init__(self):
QDialog.__init__(self)
self.setLayout(QVBoxLayout())
self.layout().setSpacing (0)
self.layout().setContentsMargins(0,0,0,0);
btn = QPushButton()
self.layout().addWidget(btn)
self.exec_()
a = c()
您需要设置尺寸策略才能展开。 我不知道 python 中的语法,但你可以轻松地从 C++ 翻译过来。
QPushButton* button = new QPushButton(this);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
The C++ documentation of the QBoxLayout Class 表示
QBoxLayout::addWidget(widget, stretch=0, alignment=0)
If the stretch factor is 0 and nothing else in the QBoxLayout has a stretch factor greater than zero, the space is distributed according to the QWidget:sizePolicy() of each widget that's involved.
所以看看 QPushButton
Policy
>>> btn = QPushButton()
>>> btn.sizePolicy().horizontalPolicy()
1
>>> btn.sizePolicy().verticalPolicy()
0
我们会发现,垂直策略是固定的。添加像
这样的按钮[...]
btn = QPushButton()
policy = btn.sizePolicy().horizontalPolicy()
btn.setSizePolicy(policy, policy)
self.layout().addWidget(btn)
[...]
现在将通过您的对话框自动调整按钮。