两个QTextEdit,pyqt4,python的单个滚动条

时间:2016-02-23 18:12:43

标签: python-3.x scrollbar pyqt4 qtextedit

如何为两个QTextEdit,pyqt4,python制作一个滚动条。或者如何同步两个QTextEdit的两个滚动条。用于同时滚动文本。 Pyqt4,python。

1 个答案:

答案 0 :(得分:1)

交叉连接所有滚动条的value changed signals

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.edit1 = QtGui.QTextEdit(self)
        self.edit2 = QtGui.QTextEdit(self)
        layout = QtGui.QHBoxLayout(self)
        layout.addWidget(self.edit1)
        layout.addWidget(self.edit2)
        self.edit1.horizontalScrollBar().valueChanged.connect(
            self.edit2.horizontalScrollBar().setValue)
        self.edit1.verticalScrollBar().valueChanged.connect(
            self.edit2.verticalScrollBar().setValue)
        self.edit2.horizontalScrollBar().valueChanged.connect(
            self.edit1.horizontalScrollBar().setValue)
        self.edit2.verticalScrollBar().valueChanged.connect(
            self.edit1.verticalScrollBar().setValue)
        text = '\n'.join(name for name in dir(QtCore))
        self.edit1.setText(text)
        self.edit2.setText(text)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 600, 400)
    window.show()
    sys.exit(app.exec_())