4

I want to do this layout for my window:

enter image description here

So I tried to create a QHBoxLayout layout to put the 3 buttons, and add it to the 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()

But it is not accepting another layout:

verticalLayout.addWidget( horizontalLayout )
TypeError: addWidget(self, QWidget, stretch: int = 0, alignment: Qt.Alignment = 0): argument 1 has unexpected type 'QHBoxLayout'

How to do this this alignment?


Update

By @G.M. comment, using addLayout() I got this warning on the console:

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

But now the window was displayed without the edit box:

enter image description here

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
  • You can't pass a `QLayout` to `QVBoxLayout::addWidget`. Use `QVBoxLayout::addLayout` instead. – G.M. May 28 '17 at 19:53
  • @Thanks, but now it do a warning `Attempting to set QLayout "" on Example "", which already has a layout`. – Evandro Coan May 28 '17 at 21:10
  • I think you've changed the wrong line. You should change `verticalLayout.addWidget( horizontalLayout )` to `verticalLayout.addLayout(horizontalLayout)`. – G.M. May 28 '17 at 21:15
  • I changed as you said to. http://i.imgur.com/9MRtU6d.gif – Evandro Coan May 28 '17 at 22:22

1 Answers1

8

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

enter image description here

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

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thanks, I just had used labels instead of buttons because they were the first thing I had to copy and create a quick example. I could just not put the elements combining the layouts formats. – Evandro Coan May 29 '17 at 11:33