python - PyQt:表单布局内的 QLineEdit 小部件放置

标签 python macos qt pyqt qtgui

使用 .addRow() 方法将 QtGui.QLineEdit line_edit 小部件放置在 QtGui.QFormLayout 表单布局内。

my_formLayout.addRow(my_label, my_lineEdit) 

要使 line_edit 小部件粘在对话框窗口的边缘(以便它随对话框调整大小),请尝试使用 sizePolicy:

    sizePolicy = my_lineEdit.sizePolicy()
    sizePolicy.setHorizontalStretch(1)
    my_lineEdit.setSizePolicy( sizePolicy )

没有错误。但是 line_edit 小部件仍然没有粘在对话框的边缘...可能出了什么问题?

最佳答案

您不需要做任何事情。

这个简单的示例根据需要调整大小:

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 的行为依赖于平台。引用docs :

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 上的默认行为。所以尝试一下:

    my_formLayout.setFieldGrowthPolicy(QtGui.QFormLayout.ExpandingFieldsGrow)

或者:

    my_formLayout.setFieldGrowthPolicy(QtGui.QFormLayout.AllNonFixedFieldsGrow)

关于python - PyQt:表单布局内的 QLineEdit 小部件放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21467262/

相关文章:

python - 如何遍历整个目录?

python - 最高计数...将输出的元组格式返回到字典格式

macos - openmpi 忽略错误 : mca interface is not recognized

cocoa - 如何在cocoa osx中检测网络事件

macos - 每当我在 Mac 上输入 pip install 时,都会收到此错误 : fish: 'pip install tweepy' terminated by signal SIGABRT (Abort)

python - Pyspark:将具有嵌套结构的数组转换为字符串

python - 如何使用 Python 解压缩 gz 文件

qt - qDebug 不显示 __FILE__,__LINE__

c++ - 我应该在 Qt Creator 中更改什么以使用 MinGW 而不是 Microsoft 编译器进行编译

c++ - 使用命名空间的 QT 测试