How can I change the width of the scrollbar of QWebView?

The stylesheet API does not yet support styling QWebView [doc.qt.nokia.com], but you can style the webview’s scrollbar using the style API.

You can subclass your style (or create a proxy style) and reimplement QStyle::pixelMetric() [doc.qt.nokia.com] to return the width you want when the metric is QStyle::PM_ScrollBarExtent [doc.qt.nokia.com]. QWebView does not actually use a QScrollBar [doc.qt.nokia.com], so you need to be a bit sneaky here to get this to work. When QStyle::pixelMetric() is called in this case, the QWidget* [doc.qt.nokia.com] passed in is 0 whereas it is the scrollbar in other cases. So you could set the style on the application and check if the widget is 0 in QStyle::pixelMetric(). If it is, you can assume it’s the webview asking for it and therefore returning your modified size and if not fall back to the base class.

The example below demonstrates how this can be done:

  1. #include <QtGui>
  2. #include <QWebView>
  3.  
  4. class Style : public QWindowsStyle
  5. {
  6. public:
  7.   Style()
  8.   {
  9.   }
  10.   int pixelMetric ( PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const
  11.   {
  12.     if (metric == QStyle::PM_ScrollBarExtent && widget == 0)
  13.       return 30;
  14.     return QWindowsStyle::pixelMetric(metric, option, widget);
  15.   }
  16.  
  17. };
  18. class WebView : public QWebView
  19. {
  20. public:
  21.   WebView()
  22.   {
  23.     qApp->setStyle(new Style());
  24.     load(QUrl("http://qt.nokia.com/doc/4.5/qwebview.html"));
  25.     QTimer::singleShot(5000, this, SLOT(testSlot()));
  26.   }
  27.  
  28. };
  29.  
  30. int main(int argc, char **argv)
  31. {
  32.   QApplication app(argc, argv);
  33.   WebView view;
  34.   view.show();
  35.   return app.exec();
  36. }
  37.  

2 comments

December 17, 2011

Picture of kinjalp kinjalp

Lab Rat

its give error
int pixelMetric ( PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const

line how to use this please give example with link i can try and change in my programe…

Please as soon as posible give me answer….

December 17, 2011

Picture of kinjalp kinjalp

Lab Rat

where we have to write this code in header file or .cpp file….

Or this is in built function in the QT itself we have to use only tell me….

tell me as soon as possible Please…..:)

kinjal

Write a comment

Sorry, you must be logged in to post a comment.