[英]How to apply QScrollerProperties to a QScroller, to get rid of overshoot?
As the title says I'm trying to make a scrollArea that uses QScroller with grabgesture so I can scroll by dragging on the widget.正如标题所说,我正在尝试制作一个使用 QScroller 和抓取的 scrollArea,以便我可以通过拖动小部件来滚动。 I found some good examples and got it working.我找到了一些很好的例子并让它工作。 Now I want to remove the overshoot that happens when you drag further than there is items in the widget.现在我想消除当你拖得比小部件中的项目更远时发生的过冲。
But when I try to tweak the Qscroller, I can't seem to figure out how to apply the QScrollerProperties to the QScroller.但是当我尝试调整 Qscroller 时,我似乎无法弄清楚如何将 QScrollerProperties 应用于 QScroller。 Which is how I assume you remove the overshoot.这就是我假设您消除过冲的方式。
Here is an example of the code:以下是代码示例:
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_())
I'm not sure how to proceed from here.我不知道如何从这里开始。 Any help would be appriciated :)任何帮助都会得到帮助:)
Just call scroll.setScrollerProperties(props)
once you've set the new properties.设置新属性后,只需调用scroll.setScrollerProperties(props)
即可。
When you call scrollerProperties()
you get "copy" of the current properties: it is not a pointer to the actual properties, so nothing changes unless you apply them back to the scroller.当您调用scrollerProperties()
您将获得当前属性的“副本”:它不是指向实际属性的指针,因此除非您将它们应用回滚动条,否则不会发生任何变化。
It's almost like calling self.font()
:这几乎就像调用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)
The same applies to almost any property, like text()
/ setText()
for labels, palette()
/ setPalette()
, etc.这同样适用于几乎所有属性,例如text()
/ setText()
用于标签、 palette()
/ setPalette()
等。
To prevent the vertical overshoot, you have to use setScrollMetric
with VerticalOvershootPolicy
, and set the value to OvershootAlwaysOff:为了防止垂直过冲,您必须将setScrollMetric
与VerticalOvershootPolicy
一起使用,并将值设置为 OvershootAlwaysOff:
props.setScrollMetric(QScrollerProperties.VerticalOvershootPolicy,
QScrollerProperties.OvershootAlwaysOff)
scroll.setScrollerProperties(props)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.