[英]PyQt: QLineEdit with attached label
This is probably a very silly question, but anyway I am struck by it :-( 这可能是一个很愚蠢的问题,但是无论如何我都被它震惊了:-(
I have a window with two side-by-side lists and a line edit control with a label below them (see the attached image). 我有一个带有两个并排列表的窗口,以及一个在其下方带有标签的行编辑控件(请参见下图)。 What I want is to have the line edit field placed just after the corresponding label and not below the second list.
我想要的是将行编辑字段放置在相应标签之后,而不在第二个列表下方。
Here is my test code: 这是我的测试代码:
import sys
from PyQt4 import Qt, QtCore, QtGui
class MainWindow(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
centralLayout = QtGui.QGridLayout()
centralLayout.setSpacing(2)
treelist1 = QtGui.QTreeWidget()
treelist2 = QtGui.QTreeWidget()
treelist1.setColumnCount(1)
treelist1.setHeaderLabels(["List 1"])
treelist2.setColumnCount(1)
treelist2.setHeaderLabels(["List 2"])
label = QtGui.QLabel("Line:")
linedit = QtGui.QLineEdit()
centralLayout.setRowStretch(0, 8)
centralLayout.setRowStretch(1, 1)
centralLayout.addWidget(treelist1, 0, 0)
centralLayout.addWidget(treelist2, 0, 1)
centralLayout.addWidget(label, 1, 0)
centralLayout.addWidget(linedit, 1, 1)
self.setLayout(centralLayout)
self.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mw = MainWindow()
sys.exit(app.exec_())
Thanks in advance for any assistance you can provide! 预先感谢您可以提供的任何帮助!
What you need here is: 您需要的是:
Here is the answer to my question, based on the suggestion by ypnos: 根据ypnos的建议,这是我的问题的答案:
import sys
from PyQt4 import Qt, QtCore, QtGui
class MainWindow(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
verticalLayout = QtGui.QVBoxLayout()
verticalLayout.setSpacing(2)
horizontalLayout1 = QtGui.QHBoxLayout()
horizontalLayout2 = QtGui.QHBoxLayout()
treelist1 = QtGui.QTreeWidget()
treelist2 = QtGui.QTreeWidget()
treelist1.setColumnCount(1)
treelist1.setHeaderLabels(["List 1"])
treelist2.setColumnCount(1)
treelist2.setHeaderLabels(["List 2"])
label = QtGui.QLabel("Line:")
linedit = QtGui.QLineEdit()
horizontalLayout1.addWidget(treelist1)
horizontalLayout1.addWidget(treelist2)
horizontalLayout2.addWidget(label)
horizontalLayout2.addWidget(linedit)
verticalLayout.addLayout(horizontalLayout1)
verticalLayout.addLayout(horizontalLayout2)
self.setLayout(verticalLayout)
self.show()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mw = MainWindow()
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.