我想连接分离器以在4个小部件之间建立一个“交叉”分离器。我尝试使用两个分离器之间的信号和插槽来做到这一点,即使没有发生错误,其行为也不是我希望的(实际上什么也没有发生)。

import sys
from PyQt4 import QtGui, QtCore

class ApplicationWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.initUI()

        self.window.setFocus()
        self.setCentralWidget(self.window)
        self.showMaximized()

    def initUI(self) :
        self.window = QtGui.QWidget()

        self.editor1 = QtGui.QTextEdit()
        self.editor2 = QtGui.QTextEdit()
        self.editor3 = QtGui.QTextEdit()
        self.editor4 = QtGui.QTextEdit()

        self.split1 = QtGui.QSplitter()
        self.split2 = QtGui.QSplitter()
        self.split3 = QtGui.QSplitter()
        self.split2.setOrientation(QtCore.Qt.Vertical)
        self.split3.setOrientation(QtCore.Qt.Vertical)

        self.split2.addWidget(self.editor1)
        self.split2.addWidget(self.editor2)
        self.split3.addWidget(self.editor3)
        self.split3.addWidget(self.editor4)

        self.connect(self.split2, QtCore.SIGNAL("splitterMoved"), self.split3.moveSplitter)
        self.connect(self.split3, QtCore.SIGNAL("splitterMoved"), self.split2.moveSplitter)

        self.split1.addWidget(self.split2)
        self.split1.addWidget(self.split3)

        self.layout = QtGui.QHBoxLayout()
        self.layout.addWidget(self.split1)
        self.window.setLayout(self.layout)

def main() :
    qApp = QtGui.QApplication(sys.argv)
    qApp.setStyle('cleanlooks')
    aw = ApplicationWindow()
    aw.show()
    sys.exit(qApp.exec_())

if __name__ == '__main__':
    main()


有任何想法吗 ?

最佳答案

好的,最后我找到了如何使用python做到这一点:

import sys
from PyQt4 import QtGui, QtCore

class ApplicationWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("Cluster View")
        self.initUI()

        self.window.setFocus()
        self.setCentralWidget(self.window)
        self.showMaximized()

    def splitterMoved(self, sender) :
        print ("ok", sender)
        receiver = self.split2 if sender is self.split3 else self.split3
        receiver.blockSignals(True)
        receiver.setSizes(sender.sizes())
        receiver.blockSignals(False)

    def initUI(self) :
        self.window = QtGui.QWidget()

        self.editor1 = QtGui.QTextEdit()
        self.editor2 = QtGui.QTextEdit()
        self.editor3 = QtGui.QTextEdit()
        self.editor4 = QtGui.QTextEdit()

        self.split1 = QtGui.QSplitter()
        self.split2 = QtGui.QSplitter()
        self.split3 = QtGui.QSplitter()
        self.split2.setOrientation(QtCore.Qt.Vertical)
        self.split3.setOrientation(QtCore.Qt.Vertical)

        self.split2.addWidget(self.editor1)
        self.split2.addWidget(self.editor2)
        self.split3.addWidget(self.editor3)
        self.split3.addWidget(self.editor4)

        self.connect(self.split2, QtCore.SIGNAL("splitterMoved(int, int)"), lambda x : self.splitterMoved(self.split2))
        self.connect(self.split3, QtCore.SIGNAL("splitterMoved(int, int)"), lambda x : self.splitterMoved(self.split3))

        self.split1.addWidget(self.split2)
        self.split1.addWidget(self.split3)

        self.layout = QtGui.QHBoxLayout()
        self.layout.addWidget(self.split1)
        self.window.setLayout(self.layout)

def main() :
    qApp = QtGui.QApplication(sys.argv)
    qApp.setStyle('cleanlooks')
    aw = ApplicationWindow()
    aw.show()
    sys.exit(qApp.exec_())

if __name__ == '__main__':
    main()


也许有更好的方法可以做到这一点,但至少现在可以了。

10-07 14:25