[英]How to apply QScrollerProperties to a QScroller, to get rid of overshoot?
正如標題所說,我正在嘗試制作一個使用 QScroller 和抓取的 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()
您將獲得當前屬性的“副本”:它不是指向實際屬性的指針,因此除非您將它們應用回滾動條,否則不會發生任何變化。
這幾乎就像調用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.