PyQt:LineEdit小部件在FormLayout内的位置 [英] PyQt: LineEdit widget's placement inside of FormLayout
问题描述
使用.addRow()方法将 QtGui.QLineEdit line_edit小部件放置在 QtGui.QFormLayout 表单布局中.
A QtGui.QLineEdit line_edit widget is placed inside of QtGui.QFormLayout Form layout using .addRow() method.
my_formLayout.addRow(my_label, my_lineEdit)
要使line_edit小部件粘贴到对话框窗口的边缘(以便随对话框调整大小),请尝试使用sizePolicy:
To make a line_edit widget to stick to a dialog window's edges (so it re-sizes with the dialog) tried using sizePolicy:
sizePolicy = my_lineEdit.sizePolicy()
sizePolicy.setHorizontalStretch(1)
my_lineEdit.setSizePolicy( sizePolicy )
没有错误.但是line_edit窗口小部件仍然没有贴在对话框的边缘...可能是什么问题?
There are no errors. But the line_edit widget still doesn't stick to the edges of the dialog... What could be wrong?
推荐答案
您无需执行任何操作.
这个简单的示例将根据需要调整大小:
This simple example resizes as necessary:
from PyQt4 import QtGui
class Dialog(QtGui.QDialog):
def __init__(self):
super(Dialog, self).__init__()
form = QtGui.QFormLayout(self)
label = QtGui.QLabel('Label', self)
edit = QtGui.QLineEdit(self)
form.addRow(label, edit)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Dialog()
window.setGeometry(500, 300, 300, 50)
window.show()
sys.exit(app.exec_())
更新:
好的,看来QFormaLayout 的行为与平台有关.引用文档:
Okay, it seems the behaviour of QFormaLayout is platform-dependent. To quote from the docs:
基于Mac OS X Aqua准则的样式.标签是右对齐的,字段不会超出其大小提示,并且表单是水平居中的.
Style based on the Mac OS X Aqua guidelines. Labels are right-aligned, the fields don't grow beyond their size hint, and the form is horizontally centered.
但是,有一个 setFieldGrowthPolicy 方法,可以用来替代Mac OSX上的默认行为.因此,尝试:
However, there is a setFieldGrowthPolicy method, which could be used to over-ride the default behaviour on Mac OSX. So try:
my_formLayout.setFieldGrowthPolicy(QtGui.QFormLayout.ExpandingFieldsGrow)
或:
my_formLayout.setFieldGrowthPolicy(QtGui.QFormLayout.AllNonFixedFieldsGrow)
这篇关于PyQt:LineEdit小部件在FormLayout内的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!