通过鼠标滚轮平滑滚动 QTableWidget
Scrolling QTableWidget smoothly BY MOUSE WHEEL
我试过 .setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
效果很好,但需要用户将鼠标移动到滚动条并使用它来体验平滑滚动,但鼠标滚轮以旧方式工作,滚动不稳定,我想知道是否有使用鼠标滚轮时如何使滚动行为相同?
你应该使用 self.widget.verticalScrollBar().setSingleStep(step).
QTableWidget继承QTableView,QAbstractItemView继承QAbstractItemView,QAbstractScrollArea继承方法verticalScrollBar(),QScrollBar继承QAbstractSliderClass,最后有setSingleStep(step)方法(可能有)是更短的路径吗?)。
完整代码如下:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setWindowTitle("Scrolling QTableWidget smoothly BY MOUSE WHEEL")
label = QLabel("singleStep:")
self.spinbox = QSpinBox()
self.spinbox.setValue(1)
self.spinbox.setMinimum(1)
self.spinbox.setMaximum(200)
self.spinbox.valueChanged.connect(self.on_value_changed)
self.widget = QTableWidget(100, 5)
for i in range(100):
for j in range(5):
self.widget.setItem(i, j, QTableWidgetItem(str(i+j)))
self.widget.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel)
#self.widget.verticalScrollBar().setSingleStep(1)
self.set_single_step()
spinbox_layout = QHBoxLayout()
spinbox_layout.addStretch()
spinbox_layout.addWidget(label)
spinbox_layout.addWidget(self.spinbox)
layout = QVBoxLayout()
layout.addLayout(spinbox_layout)
layout.addWidget(self.widget)
self.setLayout(layout)
def on_value_changed(self, step):
self.set_single_step()
def set_single_step(self):
self.widget.verticalScrollBar().setSingleStep(self.spinbox.value())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.resize(800, 600)
window.show()
sys.exit(app.exec())
您可以 increase/decrease 进入旋转框以查看其行为方式。我希望这就是你所要求的。
我试过 .setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
效果很好,但需要用户将鼠标移动到滚动条并使用它来体验平滑滚动,但鼠标滚轮以旧方式工作,滚动不稳定,我想知道是否有使用鼠标滚轮时如何使滚动行为相同?
你应该使用 self.widget.verticalScrollBar().setSingleStep(step).
QTableWidget继承QTableView,QAbstractItemView继承QAbstractItemView,QAbstractScrollArea继承方法verticalScrollBar(),QScrollBar继承QAbstractSliderClass,最后有setSingleStep(step)方法(可能有)是更短的路径吗?)。
完整代码如下:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setWindowTitle("Scrolling QTableWidget smoothly BY MOUSE WHEEL")
label = QLabel("singleStep:")
self.spinbox = QSpinBox()
self.spinbox.setValue(1)
self.spinbox.setMinimum(1)
self.spinbox.setMaximum(200)
self.spinbox.valueChanged.connect(self.on_value_changed)
self.widget = QTableWidget(100, 5)
for i in range(100):
for j in range(5):
self.widget.setItem(i, j, QTableWidgetItem(str(i+j)))
self.widget.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel)
#self.widget.verticalScrollBar().setSingleStep(1)
self.set_single_step()
spinbox_layout = QHBoxLayout()
spinbox_layout.addStretch()
spinbox_layout.addWidget(label)
spinbox_layout.addWidget(self.spinbox)
layout = QVBoxLayout()
layout.addLayout(spinbox_layout)
layout.addWidget(self.widget)
self.setLayout(layout)
def on_value_changed(self, step):
self.set_single_step()
def set_single_step(self):
self.widget.verticalScrollBar().setSingleStep(self.spinbox.value())
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.resize(800, 600)
window.show()
sys.exit(app.exec())
您可以 increase/decrease 进入旋转框以查看其行为方式。我希望这就是你所要求的。