QSplitter show a divider or a margin between the two widgets

前端 未结 4 1109
情深已故
情深已故 2021-02-14 12:05

I have a QSplitter and two widgets on either side, but I want to be able to have a margin, so that there is a clear transition between the two widgets. I looked in QSplitter and

4条回答
  •  甜味超标
    2021-02-14 12:42

    Here's how you can overwrite the QSplitter class to make the splitters to have some icon to let user know they can resize the widgets, See the image for output. enter image description here

    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    import sys
    
    class Handle(QWidget):
        def paintEvent(self, e=None):
            painter = QPainter(self)
            painter.setPen(Qt.NoPen)
            painter.setBrush(Qt.Dense6Pattern)
            painter.drawRect(self.rect())
    
    class customSplitter(QSplitter):
        def addWidget(self, wdg):
            super().addWidget(wdg)
            self.width = self.handleWidth()
            l_handle = Handle()
            l_handle.setMaximumSize(self.width*2, self.width*10)
            layout = QHBoxLayout(self.handle(self.count()-1))
            layout.setSpacing(0)
            layout.setContentsMargins(0,0,0,0)
            layout.addWidget(l_handle)
    
    class Window(QMainWindow):
        def setUI(self, MainWindow):
            self.splt_v = customSplitter(Qt.Vertical)
            self.splt_v.setHandleWidth(8)
            self.splt_v.addWidget(QGroupBox("Box 1"))
            self.splt_v.addWidget(QGroupBox("Box 2"))
            self.splt_v.addWidget(QGroupBox("Box 3"))
    
            self.wdg = QWidget()
            self.v_lt = QVBoxLayout(self.wdg)
            self.v_lt.addWidget(self.splt_v)
    
            self.spl_h = customSplitter()
            self.spl_h.addWidget(self.wdg)
            self.spl_h.addWidget(QGroupBox("Box 4"))
            self.spl_h.addWidget(QGroupBox("Box 5"))
    
            self.h_lt = QHBoxLayout()
            self.h_lt.addWidget(self.spl_h)
            self.w = QWidget()
            self.w.setLayout(self.h_lt)
            self.w.setGeometry(0,0,1280,720)
            self.w.show()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        MainWindow = QMainWindow()
        ui = Window()
        ui.setUI(MainWindow)
        sys.exit(app.exec_())
    

提交回复
热议问题