【问题标题】:How to align the layouts QHBoxLayout and QVBoxLayout?如何对齐布局 QHBoxLayout 和 QVBoxLayout?
【发布时间】: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

标签: python pyqt pyqt4 qlayout


【解决方案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()

【讨论】:

  • 谢谢,我刚刚使用了标签而不是按钮,因为它们是我必须复制并创建一个快速示例的第一件事。我不能把元素结合布局格式。
猜你喜欢
  • 2012-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-25
  • 2021-07-13
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
相关资源
最近更新 更多