How to align the layouts QHBoxLayout and QVBoxLayout?

*爱你&永不变心* 提交于 2019-12-06 08:28:29

The problem you show in your update is generated because you have to self as your parent, and this is placed in that widget, a simple solution is change to:

horizontalLayout = QtGui.QHBoxLayout()

complete code:

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_() )

Another question is that you talk about buttons, in addition that the graph shows a widget of almost square dimensions, I think if you want to get it you should use QTextEdit.

Complete code:

#!/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()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!