如何将 QScrollerProperties 应用于 QScroller,以消除超调?
How to apply QScrollerProperties to a QScroller, to get rid of overshoot?
正如标题所说,我正在尝试制作一个使用 QScroller 和 grabgesture 的 scrollArea,这样我就可以通过拖动小部件来滚动。我找到了一些很好的例子并让它发挥作用。现在我想消除当您拖动的距离超过小部件中的项目时发生的超调。
但是当我尝试调整 Qscroller 时,我似乎无法弄清楚如何将 QScrollerProperties 应用于 QScroller。这就是我假设您消除过冲的方式。
这是代码示例:
import sys
from PyQt5.QtWidgets import (
QApplication,
QFormLayout,
QGridLayout,
QLabel,
QScrollArea,
QScroller,
QScrollerProperties,
QWidget,
)
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
scroll_area = QScrollArea()
layout = QGridLayout(self)
layout.addWidget(scroll_area)
scroll_widget = QWidget()
scroll_layout = QFormLayout(scroll_widget)
for i in range(200):
scroll_layout.addRow(QLabel('Label #{}'.format(i)))
scroll_area.setWidget(scroll_widget)
scroll = QScroller.scroller(scroll_area.viewport())
scroll.grabGesture(scroll_area.viewport(), QScroller.LeftMouseButtonGesture)
scroll.scrollerPropertiesChanged.connect(self.PropsChanged) #Just to see if I could registre a change
props = scroll.scrollerProperties()
props.setScrollMetric(QScrollerProperties.VerticalOvershootPolicy,QScrollerProperties.OvershootAlwaysOff)
props.setScrollMetric(QScrollerProperties.DragStartDistance, 0.01)
#Apply Qscroller properties here somehow?
print(scroll.scrollerProperties().scrollMetric(QScrollerProperties.DragStartDistance))
scroll.scrollerProperties = props #Maybe? Doesn't seem to change the overshoot?
def PropsChanged(self):
print("Something is being changed??")
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
我不确定如何从这里开始。
任何帮助将不胜感激:)
设置新属性后,只需调用 scroll.setScrollerProperties(props)
。
当您调用 scrollerProperties()
时,您会获得 "copy" 当前属性:它不是指向实际属性的 指针 ,因此除非您将它们应用回滚动条。
这几乎就像调用 self.font()
:
font = self.font()
font.setPointSize(20)
# at this point, the widget font is still the same...
# unless you do this:
self.setFont(font)
这同样适用于几乎所有 属性,例如 text()
/setText()
用于标签,palette()
/setPalette()
,等等
为了防止垂直过冲,您必须使用 setScrollMetric
和 VerticalOvershootPolicy
,并将值设置为 OvershootAlwaysOff:
props.setScrollMetric(QScrollerProperties.VerticalOvershootPolicy,
QScrollerProperties.OvershootAlwaysOff)
scroll.setScrollerProperties(props)
正如标题所说,我正在尝试制作一个使用 QScroller 和 grabgesture 的 scrollArea,这样我就可以通过拖动小部件来滚动。我找到了一些很好的例子并让它发挥作用。现在我想消除当您拖动的距离超过小部件中的项目时发生的超调。
但是当我尝试调整 Qscroller 时,我似乎无法弄清楚如何将 QScrollerProperties 应用于 QScroller。这就是我假设您消除过冲的方式。
这是代码示例:
import sys
from PyQt5.QtWidgets import (
QApplication,
QFormLayout,
QGridLayout,
QLabel,
QScrollArea,
QScroller,
QScrollerProperties,
QWidget,
)
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
scroll_area = QScrollArea()
layout = QGridLayout(self)
layout.addWidget(scroll_area)
scroll_widget = QWidget()
scroll_layout = QFormLayout(scroll_widget)
for i in range(200):
scroll_layout.addRow(QLabel('Label #{}'.format(i)))
scroll_area.setWidget(scroll_widget)
scroll = QScroller.scroller(scroll_area.viewport())
scroll.grabGesture(scroll_area.viewport(), QScroller.LeftMouseButtonGesture)
scroll.scrollerPropertiesChanged.connect(self.PropsChanged) #Just to see if I could registre a change
props = scroll.scrollerProperties()
props.setScrollMetric(QScrollerProperties.VerticalOvershootPolicy,QScrollerProperties.OvershootAlwaysOff)
props.setScrollMetric(QScrollerProperties.DragStartDistance, 0.01)
#Apply Qscroller properties here somehow?
print(scroll.scrollerProperties().scrollMetric(QScrollerProperties.DragStartDistance))
scroll.scrollerProperties = props #Maybe? Doesn't seem to change the overshoot?
def PropsChanged(self):
print("Something is being changed??")
if __name__ == '__main__':
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
我不确定如何从这里开始。 任何帮助将不胜感激:)
设置新属性后,只需调用 scroll.setScrollerProperties(props)
。
当您调用 scrollerProperties()
时,您会获得 "copy" 当前属性:它不是指向实际属性的 指针 ,因此除非您将它们应用回滚动条。
这几乎就像调用 self.font()
:
font = self.font()
font.setPointSize(20)
# at this point, the widget font is still the same...
# unless you do this:
self.setFont(font)
这同样适用于几乎所有 属性,例如 text()
/setText()
用于标签,palette()
/setPalette()
,等等
为了防止垂直过冲,您必须使用 setScrollMetric
和 VerticalOvershootPolicy
,并将值设置为 OvershootAlwaysOff:
props.setScrollMetric(QScrollerProperties.VerticalOvershootPolicy,
QScrollerProperties.OvershootAlwaysOff)
scroll.setScrollerProperties(props)