PDA

View Full Version : QWebView with custom scrollbar



Fred
16th August 2018, 14:36
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?

https://s22.postimg.cc/p0pha7t2l/picture.png (https://postimg.cc/image/p0pha7t2l/)

Thank you for your help.
Fred



Source example:
scrollbar.h

#include <QPaintEvent>
#include <QPainter>
#include <QScrollBar>
#include <QWidget>

class scrollbar : public QScrollBar
{
Q_OBJECT
public:
scrollbar( Qt::Orientation orientation, QWidget *parent = nullptr );

protected:
void paintEvent( QPaintEvent *event );
};

scrollbar.cpp

#include "scrollbar.h"

scrollbar::scrollbar( Qt::Orientation orientation, QWidget *parent )
: QScrollBar::QScrollBar( orientation, parent )
{
}

void scrollbar::paintEvent( QPaintEvent *event )
{
int x, y, w, h;
this->rect().getRect( &x, &y, &w, &h );
QScrollBar::paintEvent( event );
QPainter p( this );
p.setPen( QPen( Qt::red, 2 ) );
p.drawLine( x, y, x + w, y + h );
}



scrollbar *_scrollBar = new scrollbar( Qt::Vertical, m_Webview1 );

Ginsengelf
17th August 2018, 08:07
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

Fred
17th August 2018, 10:52
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?
https://s22.postimg.cc/y4tere2jx/picture.png (https://postimages.org/)

wysota
17th August 2018, 11:09
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).

Fred
17th August 2018, 13:29
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 :)
https://s22.postimg.cc/ffk9cjx0d/Picture.png (https://postimages.org/)