Results 1 to 5 of 5

Thread: QWebView with custom scrollbar

  1. #1
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default QWebView with custom scrollbar

    Hi,

    I want to replace the standard scrollbar of a QWebView with my own. Therefore I made a subclass of QScrollBar.

    However, this does not work as hoped for. The subclass is displayed on the left (with the red line) and the normal scrollbar on the right. How do I get QWebView to only display and use my subclass?



    Thank you for your help.
    Fred



    Source example:
    scrollbar.h
    Qt Code:
    1. #include <QPaintEvent>
    2. #include <QPainter>
    3. #include <QScrollBar>
    4. #include <QWidget>
    5.  
    6. class scrollbar : public QScrollBar
    7. {
    8. Q_OBJECT
    9. public:
    10. scrollbar( Qt::Orientation orientation, QWidget *parent = nullptr );
    11.  
    12. protected:
    13. void paintEvent( QPaintEvent *event );
    14. };
    To copy to clipboard, switch view to plain text mode 

    scrollbar.cpp
    Qt Code:
    1. #include "scrollbar.h"
    2.  
    3. scrollbar::scrollbar( Qt::Orientation orientation, QWidget *parent )
    4. : QScrollBar::QScrollBar( orientation, parent )
    5. {
    6. }
    7.  
    8. void scrollbar::paintEvent( QPaintEvent *event )
    9. {
    10. int x, y, w, h;
    11. this->rect().getRect( &x, &y, &w, &h );
    12. QScrollBar::paintEvent( event );
    13. QPainter p( this );
    14. p.setPen( QPen( Qt::red, 2 ) );
    15. p.drawLine( x, y, x + w, y + h );
    16. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. scrollbar *_scrollBar = new scrollbar( Qt::Vertical, m_Webview1 );
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2008
    Posts
    406
    Thanks
    9
    Thanked 52 Times in 50 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWebView with custom scrollbar

    Hi, currently you have only created a scrollbar that is a child of the WebView. How should the WebView know that this is the scrollbar to use?

    Besides, it looks to me like scrolling is handled by QWebFrame (get it with m_Webview1->page()->mainFrame()), but there is also no API function to change the scrollbar.

    Ginsengelf

  3. #3
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QWebView with custom scrollbar

    Hello Ginsengelf and thanks for the answer. Maybe it is not necessary to replace the scrollbar. All I want to achieve is a similar effect to Google Chrome. Markings are inserted there optically so that you can quickly find places in the document.
    I used a QFrame to test it, but you can't put it over the scrollbar, because it is no longer being usable with the mouse.

    Are there any other ways to achieve this effect?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QWebView with custom scrollbar

    First of all I think the scrollbar is associated with QWebFrame and not QWebView so you should start looking there. However painting the scrollbar is done deep inside QtWebKit implementation so you won't be able to override that easily and you won't be able to replace the scrollbar with your own. You can however subclass QWebView, reimplement its paintEvent() and use QPainter API do paint the marks wherever you want (after calling the base class implementation of the paint event of course).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    Fred (17th August 2018)

  6. #5
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QWebView with custom scrollbar

    Thanks for your feedback wysota, that was the crucial clue. I only had to insert the code of my previous QFrame subclass into the QWebView subclass and adjust it and now it works as hoped. Very great

Similar Threads

  1. Custom ScrollBar
    By Alundra in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2014, 08:53
  2. How to address to vertical scrollbar, which appears in QWebView?
    By fromRussiaWithLove in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2011, 02:04
  3. Styling ScrollBar of QWebView via Qt StyleSheets?
    By chuckshaw in forum Qt Programming
    Replies: 5
    Last Post: 25th June 2011, 19:29
  4. Hide scrollbar in QWebView
    By kevandju in forum Qt Programming
    Replies: 0
    Last Post: 25th January 2011, 17:31
  5. QWebView ScrollBar removal
    By jay in forum Qt Programming
    Replies: 0
    Last Post: 14th May 2010, 12:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.