def update_content(self):
new_content = self.terminal.read()
new_content = self.process_backspaces(new_content)
if not new_content:
return
scrollbar = self.outputTextEdit.verticalScrollBar()
assert isinstance(scrollbar, QScrollBar)
# Preserve scroll while updating content
current_scroll = scrollbar.value()
prev_cursor = self.outputTextEdit.textCursor()
self.outputTextEdit.moveCursor(QTextCursor.End)
# Use any backspaces that were left in input to delete text
cut = 0
for x in new_content:
if x != "\b":
break
self.outputTextEdit.textCursor().deletePreviousChar()
cut += 1
self.outputTextEdit.insertPlainText(new_content[cut:])
self.outputTextEdit.setTextCursor(prev_cursor)
if self._auto_scroll:
scrollbar.setValue(scrollbar.maximum())
else:
scrollbar.setValue(current_scroll)
python类QScrollBar()的实例源码
def _update_output(self):
scrollbar = self.outputEdit.verticalScrollBar()
assert isinstance(scrollbar, QScrollBar)
# Preserve scroll while updating content
current_scroll = scrollbar.value()
scrolling = scrollbar.isSliderDown()
with self._flash_output_mutex:
self.outputEdit.setPlainText(self._flash_output.decode('utf-8', errors="ignore"))
if not scrolling:
scrollbar.setValue(scrollbar.maximum())
else:
scrollbar.setValue(current_scroll)
def _scroll_released(self):
if not self.autoscrollCheckBox.isChecked():
self._auto_scroll = False
return
scrollbar = self.outputTextEdit.verticalScrollBar()
assert isinstance(scrollbar, QScrollBar)
current_scroll = scrollbar.value()
self._auto_scroll = current_scroll >= scrollbar.maximum()
def __init__(self, scene, parent):
super().__init__(scene, parent)
self.setAlignment(Qt.AlignLeft | Qt.AlignTop)
self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
self.setMouseTracking(True)
self.YScrollBar = QtWidgets.QScrollBar(Qt.Vertical, parent)
self.XScrollBar = QtWidgets.QScrollBar(Qt.Horizontal, parent)
self.setVerticalScrollBar(self.YScrollBar)
self.setHorizontalScrollBar(self.XScrollBar)
self.currentobj = None
self.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform)
def init_widget(self):
self.setWindowTitle("Hello World")
form_lbx = QBoxLayout(QBoxLayout.TopToBottom, parent=self)
self.setLayout(form_lbx)
pgsb = QProgressBar()
vscrb = QScrollBar(orientation=Qt.Horizontal)
vscrb.setRange(0, 100)
vscrb.valueChanged.connect(pgsb.setValue)
form_lbx.addWidget(pgsb)
form_lbx.addWidget(vscrb)