user5381829
user5381829

Reputation:

PyQT - Add a boxlayout to a boxlayout

I would like to create a Horizontal BoxLayout and put inside a vertical BoxLayout.

I came up with the following code that does not work: my window shows up, but the BoxLayouts are not there (at least not visible):

self.setTabText(0, "Folders")
layout1 = QHBoxLayout()
l = QLabel();
l.setPixmap(QPixmap("pics/file.png"))
text = QTextEdit("Un fichier")
element = QVBoxLayout()
element.addChildWidget(l)
element.addChildWidget(text)
layout1.addChildWidget(element)
self.tab1.setLayout(layout1)

How can I make this work ?

Upvotes: 0

Views: 1654

Answers (2)

Mike
Mike

Reputation: 131

For me, I usually assign another widget for the inner layout and it works.

self.setTabText(0, "Folders")
layout1 = QHBoxLayout()
l = QLabel();
l.setPixmap(QPixmap("pics/file.png"))
text = QTextEdit("Un fichier")
element = QVBoxLayout()
#widget = QWidget()
#widget.setLayout(element)
element.addWidget(l)
element.addWidget(text)
#layout1.addWidget(widget)
self.tab1.setLayout(layout1)

codes beginning with # are modified or added.

Upvotes: 0

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339705

You need to add some widget to the layout, such the widget itself can have another layout.

import sys
from PyQt4 import QtGui , QtCore

class Viewer(QtGui.QMainWindow):  
    def __init__(self, parent = None):
        super(Viewer, self).__init__(parent) 
        self.centralwidget = QtGui.QWidget(self)
        self.setCentralWidget(self.centralwidget)
        layout1 = QtGui.QHBoxLayout()
        self.centralwidget.setLayout(layout1)

        l = QtGui.QLabel()
        l.setPixmap(QtGui.QPixmap("folder.png"))
        text = QtGui.QTextEdit("Un fichier")

        element = QtGui.QWidget(self) 
        layout2 = QtGui.QVBoxLayout()
        element.setLayout(layout2)
        layout2.addWidget(l)
        layout2.addWidget(text)
        layout1.addWidget(element)

app = QtGui.QApplication(sys.argv)
viewer = Viewer()
viewer.show()
sys.exit(app.exec_())

enter image description here

Upvotes: 1

Related Questions