【发布时间】:2017-05-28 18:51:28
【问题描述】:
我想为我的窗口做这个布局:
所以我尝试创建一个QHBoxLayout 布局来放置3 个按钮,并将其添加到QVBoxLayout:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
title = QtGui.QLabel( 'Title' )
author = QtGui.QLabel( 'Author' )
other = QtGui.QLabel( 'Other' )
titleEdit = QtGui.QLineEdit()
horizontalLayout = QtGui.QHBoxLayout( self )
horizontalLayout.addWidget( title )
horizontalLayout.addWidget( author )
horizontalLayout.addWidget( other )
verticalLayout = QtGui.QVBoxLayout( self )
verticalLayout.addWidget( titleEdit )
verticalLayout.addWidget( horizontalLayout )
self.setLayout( verticalLayout )
self.setGeometry( 300, 300, 350, 300 )
self.setWindowTitle( 'Review' )
self.show()
def main():
app = QtGui.QApplication( sys.argv )
ex = Example()
sys.exit( app.exec_() )
if __name__ == '__main__':
main()
但它不接受其他布局:
verticalLayout.addWidget( horizontalLayout )
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Qt.Alignment = 0): argument 1 has unexpected type 'QHBoxLayout'
这个对齐怎么做?
更新
@G.M. 评论,使用addLayout() 我在控制台上收到了这个警告:
QLayout: Attempting to add QLayout "" to Example "", which already has a layout
QLayout::addChildLayout: layout "" already has a parent
QWidget::setLayout: Attempting to set QLayout "" on Example "", which already has a layout
但是现在显示的窗口没有编辑框:
【问题讨论】:
-
您不能将
QLayout传递给QVBoxLayout::addWidget。请改用QVBoxLayout::addLayout。 -
@Thanks,但现在它会发出警告
Attempting to set QLayout "" on Example "", which already has a layout。 -
我认为你改错了行。您应该将
verticalLayout.addWidget( horizontalLayout )更改为verticalLayout.addLayout(horizontalLayout)。 -
我按照你说的改了。 i.imgur.com/9MRtU6d.gif