pythonlayoutpyqt5qvboxlayout

How to set a fixed size in PyQt5 for any widget without breaking layout when resizing


Haven't found this exact problem anywhere, or maybe I just don't recognize it from others. As still pretty much a beginner in programming but not totally oblivious, I am attempting to create an application with PyQt5 but I am having trouble setting up the layout properly before doing any more serious coding in the future.

As you will see, I am trying to do my main layout by use of a QHBoxLayout with two QVBoxLayout layouts inside of it. I've thrown some random widgets into each vertical layout, which work perfectly on their own except when I try to give them a fixed size. The ListWidget and LineEdit in the left vertical layout do not stay where I want them to be or how I want them to be, when given a fixed width/height/size, once I manually resize the app windon as seen in pictures. Obviously I want them to stay put in topleft corner and any subsequent widget right under the first, second, etc. Same happens with the tabswidget whenever I try applying same.

I've toyed around with geometry, sizehints, alignments, etc. but I just can't seem to figure it out. Enclosing links to two pictures of the problem:

before resizing

after resizing

And enclosing the important part of the code:

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.initUI()
        self.createactions()
        self.createmenus()
        self.createtoolbar()
        self.container = FrameContainer()
        self.setCentralWidget(self.container)



    def initUI(self):

        self.setWindowTitle(self.title)
        self.setGeometry(100,100,800,600)
        self.statusBar().showMessage("Statusbar - awaiting user control")
        self.show()



class FrameContainer(QWidget):

    def __init__(self):
        super(QWidget, self).__init__()
        self.setContentsMargins(0,0,0,0)
        self.mainlayout = QHBoxLayout(self)
        self.mainlayout.setSpacing(0)
        self.mainlayout.setContentsMargins(0, 0, 0, 0)
        self.verticalwidgets()

        self.mainlayout.addLayout(self.box_layout1)
        self.mainlayout.addLayout(self.box_layout2)


    def verticalwidgets(self):
        # Left side
        self.box_layout1 = QVBoxLayout()
        self.box_layout1.setContentsMargins(0,0,0,0)
        self.box_layout1.setSpacing(0)
        self.list_widget = QListWidget()
        self.list_widget.setFixedSize(200,500)
        self.list_widget.sizeHintForColumn(0)
        self.someWidget3 = QLineEdit()
        self.someWidget3.setFixedWidth(200)
        self.box_layout1.addWidget(self.list_widget, Qt.AlignLeft)
        self.box_layout1.addWidget(self.someWidget3, Qt.AlignLeft)

        # Right side
        self.box_layout2 = QVBoxLayout()
        self.box_layout2.setContentsMargins(0,0,0,0)
        self.box_layout2.setGeometry(QRect(0, 0, 800, 680))
        self.tabs_widget = TabsWidget(self)
        self.box_layout2.addWidget(self.tabs_widget)


class TabsWidget(QWidget):

    def __init__(self, child):
        super(QWidget, self).__init__()
        self.layout = QHBoxLayout(self)
        self.layout.setContentsMargins(0,0,0,0)


        self.tabs = QTabWidget()
        self.tabs.setTabsClosable(True)
        self.tab1 = QWidget()
        self.tab2 = QWidget()
        self.tab3 = QWidget()

        # Add tabs
        self.tabs.addTab(self.tab1, "Tab 1")
        self.tabs.addTab(self.tab2, "Tab 2")
        self.tabs.addTab(self.tab3, "Tab 3")

        # Create first tab
        self.tab1.layout = QVBoxLayout(self)
        self.pushButton1 = QPushButton("Button 1")
        self.pushButton2 = QPushButton("Button 2")
        self.pushButton3 = QPushButton("Button 3")
        self.tab1.layout.addWidget(self.pushButton1)
        self.tab1.layout.addWidget(self.pushButton2)
        self.tab1.layout.addWidget(self.pushButton3)
        self.tab2.layout = QVBoxLayout(self)
        self.tab2.layout.addWidget(QLabel("Peterpaned"))
        self.tab2.layout.addWidget(QLineEdit())
        self.tab1.setLayout(self.tab1.layout)
        self.tab2.setLayout(self.tab2.layout)

        # Add tabs to widget
        self.layout.addWidget(self.tabs)

What am I doing wrong? Don't need to necessarily give me the code answer, unless you feel like it, a serious nudge in the right direction would do I think, or at least I hope it would.


Solution

  • You can add spacers in your layout (horizontal or vertical, depending where you put them) and set a non-fixed size policy for them. This way whenever the window is resized only the spacers will stretch (or shorten).