如何对齐布局 QHBoxLayout 和 QVBoxLayout?
How to align the layouts QHBoxLayout and QVBoxLayout?
我想为我的window做这个布局:
所以我尝试创建一个 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
但是现在 window 没有显示编辑框:
您在更新中显示的问题是因为您必须将自己作为您的parent而产生的,并且它被放置在那个小部件中,一个简单的解决方案是更改为:
horizontalLayout = QtGui.QHBoxLayout()
完整代码:
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()
horizontalLayout.addWidget( title )
horizontalLayout.addWidget( author )
horizontalLayout.addWidget( other )
verticalLayout = QtGui.QVBoxLayout( self )
verticalLayout.addLayout( horizontalLayout )
verticalLayout.addWidget( titleEdit )
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_() )
另一个问题是你说的是按钮,另外图表显示了一个几乎是正方形尺寸的小部件,我想如果你想得到它你应该使用 QTextEdit
。
完整代码:
#!/usr/bin/python
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
title = QtGui.QPushButton( 'Title' )
author = QtGui.QPushButton( 'Author' )
other = QtGui.QPushButton( 'Other' )
titleEdit = QtGui.QTextEdit()
horizontalLayout = QtGui.QHBoxLayout()
horizontalLayout.addWidget( title )
horizontalLayout.addWidget( author )
horizontalLayout.addWidget( other )
verticalLayout = QtGui.QVBoxLayout( self )
verticalLayout.addLayout( horizontalLayout )
verticalLayout.addWidget( titleEdit )
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()
我想为我的window做这个布局:
所以我尝试创建一个 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
但是现在 window 没有显示编辑框:
您在更新中显示的问题是因为您必须将自己作为您的parent而产生的,并且它被放置在那个小部件中,一个简单的解决方案是更改为:
horizontalLayout = QtGui.QHBoxLayout()
完整代码:
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()
horizontalLayout.addWidget( title )
horizontalLayout.addWidget( author )
horizontalLayout.addWidget( other )
verticalLayout = QtGui.QVBoxLayout( self )
verticalLayout.addLayout( horizontalLayout )
verticalLayout.addWidget( titleEdit )
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_() )
另一个问题是你说的是按钮,另外图表显示了一个几乎是正方形尺寸的小部件,我想如果你想得到它你应该使用 QTextEdit
。
完整代码:
#!/usr/bin/python
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
title = QtGui.QPushButton( 'Title' )
author = QtGui.QPushButton( 'Author' )
other = QtGui.QPushButton( 'Other' )
titleEdit = QtGui.QTextEdit()
horizontalLayout = QtGui.QHBoxLayout()
horizontalLayout.addWidget( title )
horizontalLayout.addWidget( author )
horizontalLayout.addWidget( other )
verticalLayout = QtGui.QVBoxLayout( self )
verticalLayout.addLayout( horizontalLayout )
verticalLayout.addWidget( titleEdit )
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()