我想将表单字段放在更多列上。然而,我找不到合适的方法。
请考虑以下MWE
import sys, time
from PySide.QtGui import *
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setWindowTitle("Title")
self.setGeometry(300, 300, 250, 175)
def SetLayout(self):
formLayout = QFormLayout(self)
labelAuthor = QLabel("Author")
txtAuthor = QLineEdit()
labelTitle = QLabel("Title")
txtTitle = QLineEdit()
labelBodytext1 = QLabel("Bodytext 1")
txtBodytext1 = QTextEdit()
labelBodytext2 = QLabel("Bodytext 2")
txtBodytext2 = QTextEdit()
labelBodytext3 = QLabel("Bodytext 3")
txtBodytext3 = QTextEdit()
formLayout.addRow(labelAuthor, txtAuthor)
formLayout.addRow(labelTitle, txtTitle)
formLayout.addRow(labelBodytext1, txtBodytext1)
formLayout.addRow(labelBodytext2, txtBodytext2)
formLayout.addRow(labelBodytext3, txtBodytext3)
self.setLayout(formLayout)
if __name__ == '__main__':
try:
myApp = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.SetLayout()
mainWindow.show()
myApp.exec_()
sys.exit(0)
except NameError:
print("Name Error:", sys.exc_info()[1])
except SystemExit:
print("Closing Window...")
except Exception:
print(sys.exc_info()[1])
所有字段都在一列中对齐。相反,我想将它们放在4列以上。
答案 0 :(得分:0)
FormLayout的文档说明它已固定为两列宽度。请改用GridLayout。
有关如何使用QGridLayout的更多信息,请参阅官方BasicLayouts example。