Ashwin Raju
Ashwin Raju

Reputation: 153

How to split Horizontal layout equally or of specified dimension

I want to split my window into left frame , center frame and right frame but the right frame is split vertically into top-right and bottom-right. In my code the output shows left, and center with same size but right frame with 0 size but if i drag the center frame window i can view right frame.

    hbox = QHBoxLayout(self)
    left = QFrame(self)
    left.setFrameShape(QFrame.StyledPanel)
    center = QFrame(self)
    center.setFrameShape(QFrame.StyledPanel)
    top_right = QFrame(self)
    top_right.setFrameShape(QFrame.StyledPanel)
    bottom_right = QFrame(self)
    bottom_right.setFrameShape(QFrame.StyledPanel)

    # add top-right frame and bottom-right frame into a vertical splitter
    splitter2 = QSplitter(Qt.Vertical)
    splitter2.addWidget(top_right)
    splitter2.addWidget(bottom_right)
    # add left, center, vertical splitter into horizontal splitter
    splitter1 = QSplitter(Qt.Horizontal)
    splitter1.addWidget(left)
    splitter1.addWidget(center)
    splitter1.addWidget(splitter2)

    hbox.addWidget(splitter1)
    self.setLayout(hbox)
    QApplication.setStyle(QStyleFactory.create('Cleanlooks'))
    self.setGeometry(500, 500, 750, 750)
    self.center()
    self.setWindowTitle('Example')
    self.show()

Current output:

enter image description here

Excpeted output:

enter image description here

Upvotes: 1

Views: 3373

Answers (1)

eyllanesc
eyllanesc

Reputation: 244202

One possible solution is to establish a stretch factor is the splitter on the right

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        hbox = QHBoxLayout(self)

        splitter1 = QSplitter(self)
        splitter1.setOrientation(Qt.Horizontal)

        left = QFrame(splitter1)
        left.setFrameShape(QFrame.StyledPanel)

        center = QFrame(splitter1)
        center.setFrameShape(QFrame.StyledPanel)

        splitter2 = QSplitter(splitter1)
        sizePolicy = splitter2.sizePolicy()
        sizePolicy.setHorizontalStretch(1)

        splitter2.setSizePolicy(sizePolicy)
        splitter2.setOrientation(Qt.Vertical)

        top_right = QFrame(splitter2)
        top_right.setFrameShape(QFrame.StyledPanel)
        bottom_right = QFrame(splitter2)
        bottom_right.setFrameShape(QFrame.StyledPanel)

        hbox.addWidget(splitter1)
        self.setGeometry(500, 500, 750, 750)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    app.setStyle("fusion")
    w = Widget()
    w.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions