Issue
I have a pyside6 app which has 2 TextEdit widgets and I want the 2 TextEdits to scroll in sync. The following code is used for this function:
self.scrollconnect1 = self.TextEdit_1.verticalScrollBar().valueChanged.connect(
self.TextEdit_2.verticalScrollBar().setValue)
self.scrollconnect2 = self.TextEdit_2.verticalScrollBar().valueChanged.connect(
self.TextEdit_1.verticalScrollBar().setValue)
It works fine when there are few lines in TextEdit, but if there are too many lines (about 50,000 lines) in both TextEdit, it will not precisely synchronize in same line. (Especially when I scroll the widget by both mouse wheel and scroll bar). How to keep them synchronize precisely? Thank you!
Following code is for test:
from PySide6 import QtCore, QtGui, QtWidgets
class ScrollBar(QtWidgets.QScrollBar):
valueUpdated = QtCore.Signal(int)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setTracking(True)
self.actionTriggered.connect(self.handleAction)
def handleAction(self, action):
action = QtWidgets.QAbstractSlider.SliderAction(action)
print(self.objectName(), action, (self.value(), self.sliderPosition()))
if self.value() != self.sliderPosition():
self.valueUpdated.emit(self.sliderPosition())
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.editTop = QtWidgets.QPlainTextEdit()
self.editBtm = QtWidgets.QPlainTextEdit()
self.editTop.setVerticalScrollBar(
ScrollBar(self, objectName='EDIT-TOP'))
self.editBtm.setVerticalScrollBar(
ScrollBar(self, objectName='EDIT-BTM'))
self.editTop.verticalScrollBar().actionTriggered.connect(
lambda a: self.editBtm.verticalScrollBar().triggerAction(
QtWidgets.QAbstractSlider.SliderAction(a)))
self.editTop.verticalScrollBar().valueUpdated.connect(
self.editBtm.verticalScrollBar().setValue)
self.editBtm.verticalScrollBar().valueUpdated.connect(
self.editTop.verticalScrollBar().setValue)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.editTop)
layout.addWidget(self.editBtm)
line = ' text' * 40
# for index in range(50000):
for index in range(500):
self.editTop.insertPlainText("\n"+str(index) + line)
self.editBtm.insertPlainText("\n"+str(index) + line)
if __name__ == '__main__':
app = QtWidgets.QApplication(['Test'])
window = Window()
window.setGeometry(800, 100, 600, 400)
window.show()
app.exec()
Solution
Test Script:
from PySide6 import QtCore, QtGui, QtWidgets
class ScrollBar(QtWidgets.QScrollBar):
valueUpdated = QtCore.Signal(int)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setTracking(True)
self.actionTriggered.connect(self.handleAction)
def handleAction(self, action):
action = QtWidgets.QAbstractSlider.SliderAction(action)
print(self.objectName(), action, (self.value(), self.sliderPosition()))
if self.value() != self.sliderPosition():
self.valueUpdated.emit(self.sliderPosition())
def sliderChange(self, change):
change = QtWidgets.QAbstractSlider.SliderChange(change)
if (change == QtWidgets.QAbstractSlider.SliderValueChange and
self.signalsBlocked()):
self.blockSignals(False)
print(self.objectName(), change, self.value())
self.valueUpdated.emit(self.value())
super().sliderChange(change)
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
# self.editTop = QtWidgets.QTextEdit()
# self.editBtm = QtWidgets.QTextEdit()
self.editTop = QtWidgets.QPlainTextEdit()
self.editBtm = QtWidgets.QPlainTextEdit()
self.editTop.setVerticalScrollBar(
ScrollBar(self, objectName='EDIT-TOP'))
self.editBtm.setVerticalScrollBar(
ScrollBar(self, objectName='EDIT-BTM'))
self.editTop.verticalScrollBar().valueUpdated.connect(
self.editBtm.verticalScrollBar().setValue)
self.editBtm.verticalScrollBar().valueUpdated.connect(
self.editTop.verticalScrollBar().setValue)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.editTop)
layout.addWidget(self.editBtm)
text = ' text' * 20
# for index in range(50000):
for index in range(500):
line = f'{index}{text}\n'
self.editTop.insertPlainText(line)
self.editBtm.insertPlainText(line)
if __name__ == '__main__':
app = QtWidgets.QApplication(['Test'])
window = Window()
window.setGeometry(800, 100, 600, 400)
window.show()
app.exec()
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.