当QWebView滚动到末尾时捕获信号

发布于 2024-12-18 11:30:44 字数 125 浏览 2 评论 0原文

当用户在 QWebView 小部件中使用鼠标滚动时,我可以知道他是否到达了网页内容的开头/结尾吗?

我可以在里面放置一个 QWebView::wheelEvent() ,但是我如何知道滚动位置?

谢谢 !

When a user scroll with his mouse within a QWebView widget , can i know if he got to the head / end of web contents ?

I may place a QWebView::wheelEvent() inside , but how can i know about the scroll positions ?

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花ソ最美的依靠 2024-12-25 11:30:44

您可以检查页面主机的 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";

You can check the scrollPosition of the page's mainframe:

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";
岁月无声 2024-12-25 11:30:44

当滚动位置更改时,我在搜索实际信号时发现了这个问题。

有可以使用的 QWebPage::scrollRequested 信号。 文档说 只要内容出现,就会发出此信号rectToScroll 给出的需要向下滚动 dx 和 dy 并且没有设置视图。,但是最后一部分是错误的,信号实际上总是发出。

贡献修复了这个问题到Qt,所以这可能会被纠正为一旦文档更新。


(原帖如下)

QWebView 不提供此功能,因为 WebKit 管理滚动区域。

我最终扩展了 PaintEvent 来检查滚动位置,并在滚动位置发生变化时发出信号。

PyQt 代码发出带有百分比的 scroll_pos_changed 信号:

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)

I found this question when searching for an actual signal when the scroll position was changed.

There is the QWebPage::scrollRequested signal which can be used. The documentation says This signal is emitted whenever the content given by rectToScroll needs to be scrolled dx and dy downwards and no view was set., however the last part is wrong, the signal is actually always emitted.

I contributed a fix for this to Qt, so this will probably be corrected as soon as the docs are updated.


(original post following)

QWebView doesn't provide this because WebKit manages the scroll-area.

I ended up extending paintEvent to check the scroll position there, and emit a signal when it has changed.

PyQt code which emits a scroll_pos_changed signal with a percentage:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文