我想为我的窗口做这个布局:
所以我尝试创建一个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
但是现在窗口显示时没有编辑框:
答案 0 :(得分:1)
您在更新中显示的问题是生成的,因为您必须将自己作为父级,并将其置于该小部件中,一个简单的解决方案将更改为:
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()