ユーザーがQWebViewウィジェット内でマウスを使ってスクロールすると、Webコンテンツの先頭/末尾に到達したかどうかを知ることができますか?
QWebView :: WheelEvent()を中に配置することはできますが、スクロール位置を知るにはどうすればよいですか?
ありがとう !
scrollPosition
ページのメインフレームを確認できます。
QPoint currentPosition = webView->page()->mainFrame()->scrollPosition();
if (currentPosition.y() == webView->page()->mainFrame()->scrollBarMinimum(Qt::Vertical))
qDebug() << "Head of contents";
if (currentPosition.y() == webView->page()->mainFrame()->scrollBarMaximum(Qt::Vertical))
qDebug() << "End of contents";
スクロール位置が変更されたときの実際の信号を検索しているときに、この質問を見つけました。
QWebPage::scrollRequested
使用できる信号があります。ドキュメントには、 このシグナルは、rectToScroll によって指定されたコンテンツを dx および dy で下方向にスクロールする必要があり、ビューが設定されていないときはいつでも発行されると書かれています。ですが、最後の部分が間違っています。シグナルは実際には常に送信されます。
これに対する修正を Qt に提供したので、ドキュメントが更新されるとすぐに修正される可能性があります。
(元の投稿に続く)
WebKit がスクロール領域を管理するため、QWebView はこれを提供しません。
そこでスクロール位置を確認するために拡張paintEvent
し、変更されたときに信号を発することになりました。
scroll_pos_changed
パーセンテージでシグナルを発する PyQt コード:
class WebView(QWebView):
scroll_pos_changed = pyqtSignal(int, int)
def __init__(self, parent=None):
super().__init__(parent)
self._scroll_pos = (-1, -1)
def paintEvent(self, e):
"""Extend paintEvent to emit a signal if the scroll position changed.
This is a bit of a hack: We listen to repaint requests here, in the
hope a repaint will always be requested when scrolling, and if the
scroll position actually changed, we emit a signal..
"""
frame = self.page_.mainFrame()
new_pos = (frame.scrollBarValue(Qt.Horizontal),
frame.scrollBarValue(Qt.Vertical))
if self._scroll_pos != new_pos:
self._scroll_pos = new_pos
m = (frame.scrollBarMaximum(Qt.Horizontal),
frame.scrollBarMaximum(Qt.Vertical))
perc = (round(100 * new_pos[0] / m[0]) if m[0] != 0 else 0,
round(100 * new_pos[1] / m[1]) if m[1] != 0 else 0)
self.scroll_pos_changed.emit(*perc)
# Let superclass handle the event
return super().paintEvent(e)