2011-11-28 41 views
0

當用戶在QWebView小部件中用鼠標滾動時,我可以知道他是否到達Web內容的頭部/末端?當QWebView滾動到最後時抓住信號

我可能會放置一個QWebView :: wheelEvent()裏面,但我怎麼知道滾動位置?

謝謝!

回答

1

您可以查看網頁的主機的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"; 
0

我發現這個問題尋找一個實際信號時,當滾動位置發生了變化。

QWebPage::scrollRequested信號可以使用。 documentation says只要rectToScroll給出的內容需要滾動dx和dy向下且未設置視圖,就會發出此信號。,但最後一部分是錯誤的,信號實際上總是發射。

I contributed修復此問題到Qt,所以這可能會被更正,只要文檔被更新。因爲WebKit的管理滾動區域


(原帖如下)

QWebView不提供此。

我最終擴展了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)