Example of how to style QToolBox with QStyle

The following is an sample subclass of QWindowsXPStyle() [doc.qt.nokia.com] to show how a QToolBox [doc.qt.nokia.com] can be styled.

  1. #include <QtGui>
  2.  
  3. class MyStyle : public QWindowsXPStyle
  4. {
  5. public:
  6.     MyStyle() : QWindowsXPStyle() {}
  7.     void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter,
  8.                      const QWidget *widget = 0) const
  9.     {
  10.         if (element == CE_ToolBoxTab) {
  11.             const QStyleOptionToolBox *tb = qstyleoption_cast<const QStyleOptionToolBox *>(option);
  12.             painter->save();
  13.             painter->setBrush(QBrush(Qt::darkBlue));
  14.             painter->drawRoundedRect(option->rect.x() + 10, option->rect.y(), option->rect.width() - 20,
  15.                                      option->rect.height(), 10.0, 5.0);
  16.             painter->setPen(Qt::white);
  17.             painter->drawText(option->rect.x() + 15, option->rect.y() + 15, tb->text);
  18.             painter->restore();
  19.         } else {
  20.             return QWindowsXPStyle::drawControl(element, option, painter, widget);
  21.         }
  22.     }
  23. };
  24.  
  25. int main(int argc, char **argv)
  26. {
  27.     QApplication a(argc, argv);
  28.     a.setStyle(new MyStyle);
  29.     QToolBox tb;
  30.     tb.addItem(new QPushButton("A"), "Test");
  31.     tb.addItem(new QPushButton("B"), "Test 2");
  32.     tb.show();
  33.     return a.exec();
  34. }
  35.  

No comments

Write a comment

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