PyQt QSplitter:有没有办法将方向定义为垂直和水平?

时间:2012-09-13 15:22:34

标签: pyqt orientation splitter qsplitter

如果我希望能够在两个方向上更改分割器内物体的尺寸。

1 个答案:

答案 0 :(得分:1)

那么,这意味着你有4个小工具呢?无需自己子类和处理它,您将不得不使用3个分割器,1个垂直,2个水平:

from PyQt4.QtCore import Qt
from PyQt4.QtGui import QWidget, QApplication, QSplitter, QLabel, QVBoxLayout

class MyWidget(QWidget):
    def __init__( self, parent = None ):
        super(MyWidget, self).__init__(parent)

        self.setMinimumWidth(400)
        self.setMinimumHeight(400)

        # create widgets
        a = QLabel('A' ,self)
        b = QLabel('B', self)
        c = QLabel('C', self)
        d = QLabel('D', self)

        for lbl in (a, b, c, d):
            lbl.setAlignment(Qt.AlignCenter)

        # create 2 horizontal splitters
        h_splitter1 = QSplitter(Qt.Horizontal, self)
        h_splitter1.addWidget(a)
        h_splitter1.addWidget(b)

        h_splitter2 = QSplitter(Qt.Horizontal, self)
        h_splitter2.addWidget(c)
        h_splitter2.addWidget(d)

        h_splitter1.splitterMoved.connect(self.moveSplitter)
        h_splitter2.splitterMoved.connect(self.moveSplitter)

        self._spltA = h_splitter1
        self._spltB = h_splitter2

        # create a vertical splitter
        v_splitter = QSplitter(Qt.Vertical, self)
        v_splitter.addWidget(h_splitter1)
        v_splitter.addWidget(h_splitter2)

        layout = QVBoxLayout()
        layout.addWidget(v_splitter)
        self.setLayout(layout)

    def moveSplitter( self, index, pos ):
        splt = self._spltA if self.sender() == self._spltB else self._spltB
        splt.blockSignals(True)
        splt.moveSplitter(index, pos)
        splt.blockSignals(False)

if ( __name__ == '__main__' ):
    app = QApplication([])
    widget = MyWidget()
    widget.show()
    app.exec_()

您可以定义自己的QSplitterHandle类来控制拆分器句柄的外观,使其看起来更像“四元组”。