现在的位置: 首页 > 综合 > 正文

解析Qt自带的Style示例

2018年01月22日 ⁄ 综合 ⁄ 共 21878字 ⁄ 字号 评论关闭

http://blog.csdn.net/playstudy/article/details/7897577

在之前的QStyle类参考一文中我们介绍到实现自定义样式有两种方法:静态方法和动态方法。我们先介绍静态方法:也就是继承已经存在的类,不是QStyle,通常是QCommonStyle或者是QWindowsStyle等等。然后实现其中的虚函数,重写自己需要修改的部分代码。至于选择哪个基类来继承完全取决用户,通常选择和自己所期望的最相近的类来继承。这里贴一个图,主要是继承层次的图:


        其中的QGtkStyle就是通常GNOME环境下的,而QPlastiqueStyle则是和KDE环境最为接近的。注意QMacStyle只有在Mac OS上才有效果,因为它的实现依赖相应的平台。

       接下来解析一下Qt自带的Style示例,示例程序是Qt十分好的学习资源,不可不看哦!

首先上效果图:


                  图一:自定义样式效果图


                              图二:支持的样式

         大家可以看到该程序在我的系统(Win7)上支持很多样式,这里就不一一截图了,大家可以自己编译示例程序体验一下。

         接下来是源代码,首先看一下工程的目录结构:


        大家可以看到,实现该自定义样式的类为:NorwegianWoodStyle,而类WidgetGallery主要是用代码实现整个界面布局,并没有什么好介绍的。主要介绍一下NorwegianWoodStyle类。

1、首先是头文件:

  1. class NorwegianWoodStyle
    public QMotifStyle 
  2.     Q_OBJECT 
  3.  
  4. public
  5.     NorwegianWoodStyle() {} 
  6.  
  7.     void polish(QPalette
    &palette); 
  8.     void polish(QWidget
    *widget); 
  9.     void unpolish(QWidget
    *widget); 
  10.     int pixelMetric(PixelMetric
    metric, 
    const QStyleOption *option, 
  11.                     const QWidget
    *widget) 
    const
  12.     int styleHint(StyleHint
    hint, 
    const QStyleOption *option, 
  13.                   const QWidget
    *widget, QStyleHintReturn *returnData) 
    const
  14.     void drawPrimitive(PrimitiveElement
    element, 
    const QStyleOption *option, 
  15.                        QPainter *painter, const QWidget
    *widget) 
    const
  16.     void drawControl(ControlElement
    control, 
    const QStyleOption *option, 
  17.                      QPainter *painter, const QWidget
    *widget) 
    const
  18.  
  19. private
  20.     static void setTexture(QPalette
    &palette, QPalette::ColorRole role, 
  21.                            const QPixmap
    &pixmap); 
  22.     static QPainterPath
    roundRectPath(
    const QRect &rect); 
  23. }; 
  1. class NorwegianWoodStyle : public QMotifStyle  
  2. {  
  3.     Q_OBJECT  
  4.   
  5. public:  
  6.     NorwegianWoodStyle() {}  
  7.   
  8.     void polish(QPalette &palette);  
  9.     void polish(QWidget *widget);  
  10.     void unpolish(QWidget *widget);  
  11.     int pixelMetric(PixelMetric metric, const QStyleOption *option,  
  12.                     const QWidget *widget) const;  
  13.     int styleHint(StyleHint hint, const QStyleOption *option,  
  14.                   const QWidget *widget, QStyleHintReturn *returnData) const;  
  15.     void drawPrimitive(PrimitiveElement element, const QStyleOption *option,  
  16.                        QPainter *painter, const QWidget *widget) const;  
  17.     void drawControl(ControlElement control, const QStyleOption *option,  
  18.                      QPainter *painter, const QWidget *widget) const;  
  19.   
  20. private:  
  21.     static void setTexture(QPalette &palette, QPalette::ColorRole role,  
  22.                            const QPixmap &pixmap);  
  23.     static QPainterPath roundRectPath(const QRect &rect);  
  24. };  

        主要就是显示了QMotifStyle类中的虚函数,而这些函数最初是QStyle类中的虚函数。下面介绍这几个函数的功能:
(1)void polish(QPalette &palette)

     这是一个重载函数,功能就是改变调色板为样式指定的颜色调色板。

     该处具体实现为:

  1. //! [0] 
  2. void NorwegianWoodStyle::polish(QPalette
    &palette) 
  3.     QColor brown(212, 140, 95); 
  4.     QColor beige(236, 182, 120); 
  5.     QColor slightlyOpaqueBlack(0, 0, 0, 63); 
  6.  
  7.     QPixmap backgroundImage(":/images/woodbackground.png"); 
  8.     QPixmap buttonImage(":/images/woodbutton.png"); 
  9.     QPixmap midImage = buttonImage; 
  10.  
  11.     QPainter painter; 
  12.     painter.begin(&midImage); 
  13.     painter.setPen(Qt::NoPen); 
  14.     painter.fillRect(midImage.rect(), slightlyOpaqueBlack); 
  15.     painter.end(); 
  16. //! [0] 
  17.  
  18. //! [1] 
  19.     palette = QPalette(brown); 
  20.  
  21.     palette.setBrush(QPalette::BrightText, Qt::white); 
  22.     palette.setBrush(QPalette::Base, beige); 
  23.     palette.setBrush(QPalette::Highlight, Qt::darkGreen); 
  24.     setTexture(palette, QPalette::Button, buttonImage); 
  25.     setTexture(palette, QPalette::Mid, midImage); 
  26.     setTexture(palette, QPalette::Window, backgroundImage); 
  27.  
  28.     QBrush brush = palette.background(); 
  29.     brush.setColor(brush.color().dark()); 
  30.  
  31.     palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush); 
  32.     palette.setBrush(QPalette::Disabled, QPalette::Text, brush); 
  33.     palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush); 
  34.     palette.setBrush(QPalette::Disabled, QPalette::Base, brush); 
  35.     palette.setBrush(QPalette::Disabled, QPalette::Button, brush); 
  36.     palette.setBrush(QPalette::Disabled, QPalette::Mid, brush); 
  37. //! [1] 
  1. //! [0]  
  2. void NorwegianWoodStyle::polish(QPalette &palette)  
  3. {  
  4.     QColor brown(212, 140, 95);  
  5.     QColor beige(236, 182, 120);  
  6.     QColor slightlyOpaqueBlack(0, 0, 0, 63);  
  7.   
  8.     QPixmap backgroundImage(":/images/woodbackground.png");  
  9.     QPixmap buttonImage(":/images/woodbutton.png");  
  10.     QPixmap midImage = buttonImage;  
  11.   
  12.     QPainter painter;  
  13.     painter.begin(&midImage);  
  14.     painter.setPen(Qt::NoPen);  
  15.     painter.fillRect(midImage.rect(), slightlyOpaqueBlack);  
  16.     painter.end();  
  17. //! [0]  
  18.   
  19. //! [1]  
  20.     palette = QPalette(brown);  
  21.   
  22.     palette.setBrush(QPalette::BrightText, Qt::white);  
  23.     palette.setBrush(QPalette::Base, beige);  
  24.     palette.setBrush(QPalette::Highlight, Qt::darkGreen);  
  25.     setTexture(palette, QPalette::Button, buttonImage);  
  26.     setTexture(palette, QPalette::Mid, midImage);  
  27.     setTexture(palette, QPalette::Window, backgroundImage);  
  28.   
  29.     QBrush brush = palette.background();  
  30.     brush.setColor(brush.color().dark());  
  31.   
  32.     palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush);  
  33.     palette.setBrush(QPalette::Disabled, QPalette::Text, brush);  
  34.     palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush);  
  35.     palette.setBrush(QPalette::Disabled, QPalette::Base, brush);  
  36.     palette.setBrush(QPalette::Disabled, QPalette::Button, brush);  
  37.     palette.setBrush(QPalette::Disabled, QPalette::Mid, brush);  
  38. }  
  39. //! [1]  


(2)void polish(QWidget *widget)

       这是一个重载函数,功能是:初始化给定窗口部件的外观,该函数在每一个窗口完全创建之后的某个时刻并且是该窗口部件每一次创建后首次显示之前调用。注意,默认的实现是不做任何事情。该函数可能的动作就是调用QWidget::setBackgroundMode()。不要调用该函数来设置例如窗口部件显示位置之类的信息。重现实现这个函数使得我们获得课改变窗口部件显示方式的后门,但是Qt的样式引擎很少需要实现这个函数,而是重新实现drawItemPixmap(), drawItemText(),drawPrimitive(),等等函数来代替。

       此处的实现为:

  1. //! [3] 
  2. void NorwegianWoodStyle::polish(QWidget
    *widget) 
  3. //! [3] //! [4] 
  4.     if (qobject_cast<QPushButton
    *>(widget) 
  5.             || qobject_cast<QComboBox *>(widget)) 
  6.         widget->setAttribute(Qt::WA_Hover, true); 
  7. //! [4] 
  1. //! [3]  
  2. void NorwegianWoodStyle::polish(QWidget *widget)  
  3. //! [3] //! [4]  
  4. {  
  5.     if (qobject_cast<QPushButton *>(widget)  
  6.             || qobject_cast<QComboBox *>(widget))  
  7.         widget->setAttribute(Qt::WA_Hover, true);  
  8. }  
  9. //! [4]  


(3)int pixelMetric(PixelMetric metric, const QStyleOption *option,
                    const QWidget *widget) const

       功能:返回给定像素的公制值。指定的option和widget可以被用来计算公制值。一般情况下,widget参数都不会使用到。option参数可以通过qstyleoption_cast()函数转换为合适的类型。注意,option可能会为0,即使是PixelMetrics可以利用它。查看下面的表来获取合适的option转换:



            一些像素公制是从widgets中调用的,而一些则仅仅是style在内部调用的。如果像素公制不是从widget调用的,那么样式的制作者就应该谨慎使用它。对于一些样式而言,这也许是不适用的。

       看看该函数的具体实现:

  1. //! [7] 
  2. int NorwegianWoodStyle::pixelMetric(PixelMetric
    metric, 
  3. //! [7] //! [8] 
  4.                                     const QStyleOption
    *option, 
  5.                                     const QWidget
    *widget) 
    const 
  6.     switch (metric)
  7.     case PM_ComboBoxFrameWidth: 
  8.         return 8; 
  9.     case PM_ScrollBarExtent: 
  10.         return QMotifStyle::pixelMetric(metric,
    option, widget) + 4; 
  11.     default
  12.         return QMotifStyle::pixelMetric(metric,
    option, widget); 
  13.     } 
  14. //! [8] 
  1. //! [7]  
  2. int NorwegianWoodStyle::pixelMetric(PixelMetric metric,  
  3. //! [7] //! [8]  
  4.                                     const QStyleOption *option,  
  5.                                     const QWidget *widget) const  
  6. {  
  7.     switch (metric) {  
  8.     case PM_ComboBoxFrameWidth:  
  9.         return 8;  
  10.     case PM_ScrollBarExtent:  
  11.         return QMotifStyle::pixelMetric(metric, option, widget) + 4;  
  12.     default:  
  13.         return QMotifStyle::pixelMetric(metric, option, widget);  
  14.     }  
  15. }  
  16. //! [8]  


        这里附带说一下PixelMetric,这是一个枚举类型,主要是描述了像素公制可取的一些值,一个像素公制值是单个像素在在样式中表现的尺寸。

        在本例中我们对默认值做出了一些修改,然后就实现了自定义的样式,例如:

PM_ComboBoxFrameWidth默认值在Qt帮助文档中如下定义:

QStyle::PM_ComboBoxFrameWidth 7 Frame width of a combo box, defaults to PM_DefaultFrameWidth.

也就是说默认值是7,而在本例中我们改为8了,这就是之前说的实现虚函数,修改相应的部分实现就创建了自定义样式。


(3)int styleHint(StyleHint hint, const QStyleOption *option,
                  const QWidget *widget, QStyleHintReturn *returnData) const

        功能:对指定的widget返回由option指定的样式的hint,返回值为整型。当查询的widget需要返回更多的信息,而不仅仅是由styleHint()返回的整型值时,我们就可以使用returnData来返回这些额外的信息。

       这里的具体实现为:

  1. //! [9] 
  2. int NorwegianWoodStyle::styleHint(StyleHint
    hint, 
    const QStyleOption *option, 
  3. //! [9] //! [10] 
  4.                                   const QWidget
    *widget, 
  5.                                   QStyleHintReturn *returnData) const 
  6.     switch (hint)
  7.     case SH_DitherDisabledText: 
  8.         return int(false); 
  9.     case SH_EtchDisabledText: 
  10.         return int(true); 
  11.     default
  12.         return QMotifStyle::styleHint(hint,
    option, widget, returnData); 
  13.     } 
  14. //! [10] 
  1. //! [9]  
  2. int NorwegianWoodStyle::styleHint(StyleHint hint, const QStyleOption *option,  
  3. //! [9] //! [10]  
  4.                                   const QWidget *widget,  
  5.                                   QStyleHintReturn *returnData) const  
  6. {  
  7.     switch (hint) {  
  8.     case SH_DitherDisabledText:  
  9.         return int(false);  
  10.     case SH_EtchDisabledText:  
  11.         return int(true);  
  12.     default:  
  13.         return QMotifStyle::styleHint(hint, option, widget, returnData);  
  14.     }  
  15. }  
  16. //! [10]  

       这里也附带介绍一下StyleHint的内容,这是一个枚举类型,指定了style hints可以选取的值,一种style
hint
一般外观和/感到提示。


       由上面的代码和这里的图片可以看到这里也做了一些修改。

(5)void drawPrimitive(PrimitiveElement element, const QStyleOption *option,
                       QPainter *painter, const QWidget *widget) const

      功能:用给定的option核painter来绘制element。关于原始元素以及它们相关的option可以参见Qt帮组文档。

       这里的实现为:

  1. //! [11] 
  2. void NorwegianWoodStyle::drawPrimitive(PrimitiveElement
    element, 
  3. //! [11] //! [12] 
  4.                                        const QStyleOption
    *option, 
  5.                                        QPainter *painter, 
  6.                                        const QWidget
    *widget) 
    const 
  7.     switch (element)
  8.     case PE_PanelButtonCommand: 
  9.         { 
  10.             int delta
    = (option->state & State_MouseOver) ? 64 : 0; 
  11.             QColor slightlyOpaqueBlack(0, 0, 0, 63); 
  12.             QColor semiTransparentWhite(255, 255, 255, 127 + delta); 
  13.             QColor semiTransparentBlack(0, 0, 0, 127 - delta); 
  14.  
  15.             int x,
    y, width, height; 
  16.             option->rect.getRect(&x, &y, &width, &height); 
  17. //! [12] 
  18.  
  19. //! [13] 
  20.             QPainterPath roundRect = roundRectPath(option->rect); 
  21. //! [13] //! [14] 
  22.             int radius
    = qMin(width, height) / 2; 
  23. //! [14] 
  24.  
  25. //! [15] 
  26.             QBrush brush; 
  27. //! [15] //! [16] 
  28.             bool darker; 
  29.  
  30.             const QStyleOptionButton
    *buttonOption = 
  31.                     qstyleoption_cast<const QStyleOptionButton
    *>(option); 
  32.             if (buttonOption 
  33.                     && (buttonOption->features & QStyleOptionButton::Flat)) { 
  34.                 brush = option->palette.background(); 
  35.                 darker = (option->state & (State_Sunken | State_On)); 
  36.             } else { 
  37.                 if (option->state
    & (State_Sunken | State_On)) { 
  38.                     brush = option->palette.mid(); 
  39.                     darker = !(option->state & State_Sunken); 
  40.                 } else { 
  41.                     brush = option->palette.button(); 
  42.                     darker = false
  43. //! [16] //! [17] 
  44.                 } 
  45. //! [17] //! [18] 
  46.             } 
  47. //! [18] 
  48.  
  49. //! [19] 
  50.             painter->save(); 
  51. //! [19] //! [20] 
  52.             painter->setRenderHint(QPainter::Antialiasing, true); 
  53. //! [20] //! [21] 
  54.             painter->fillPath(roundRect, brush); 
  55. //! [21] //! [22] 
  56.             if (darker) 
  57. //! [22] //! [23] 
  58.                 painter->fillPath(roundRect, slightlyOpaqueBlack); 
  59. //! [23] 
  60.  
  61. //! [24] 
  62.             int penWidth; 
  63. //! [24] //! [25] 
  64.             if (radius
    < 10) 
  65.                 penWidth = 3; 
  66.             else if (radius
    < 20) 
  67.                 penWidth = 5; 
  68.             else 
  69.                 penWidth = 7; 
  70.  
  71.             QPen topPen(semiTransparentWhite, penWidth); 
  72.             QPen bottomPen(semiTransparentBlack, penWidth); 
  73.  
  74.             if (option->state
    & (State_Sunken | State_On)) 
  75.                 qSwap(topPen, bottomPen); 
  76. //! [25] 
  77.  
  78. //! [26] 
  79.             int x1
    = x; 
  80.             int x2
    = x + radius; 
  81.             int x3
    = x + width - radius; 
  82.             int x4
    = x + width; 
  83.  
  84.             if (option->direction
    == Qt::RightToLeft) { 
  85.                 qSwap(x1, x4); 
  86.                 qSwap(x2, x3); 
  87.             } 
  88.  
  89.             QPolygon topHalf; 
  90.             topHalf << QPoint(x1, y) 
  91.                     << QPoint(x4, y) 
  92.                     << QPoint(x3, y + radius) 
  93.                     << QPoint(x2, y + height - radius) 
  94.                     << QPoint(x1, y + height); 
  95.  
  96.             painter->setClipPath(roundRect); 
  97.             painter->setClipRegion(topHalf, Qt::IntersectClip); 
  98.             painter->setPen(topPen); 
  99.             painter->drawPath(roundRect); 
  100. //! [26] //! [32] 
  101.  
  102.             QPolygon bottomHalf = topHalf; 
  103.             bottomHalf[0] = QPoint(x4, y + height); 
  104.  
  105.             painter->setClipPath(roundRect); 
  106.             painter->setClipRegion(bottomHalf, Qt::IntersectClip); 
  107.             painter->setPen(bottomPen); 
  108.             painter->drawPath(roundRect); 
  109.  
  110.             painter->setPen(option->palette.foreground().color()); 
  111.             painter->setClipping(false); 
  112.             painter->drawPath(roundRect); 
  113.  
  114.             painter->restore(); 
  115.         } 
  116.         break
  117. //! [32] //! [33] 
  118.     default
  119. //! [33] //! [34] 
  120.         QMotifStyle::drawPrimitive(element, option, painter, widget); 
  121.     } 
  122. //! [34] 
  1. //! [11]  
  2. void NorwegianWoodStyle::drawPrimitive(PrimitiveElement element,  
  3. //! [11] //! [12]  
  4.                                        const QStyleOption *option,  
  5.                                        QPainter *painter,  
  6.                                        const QWidget *widget) const  
  7. {  
  8.     switch (element) {  
  9.     case PE_PanelButtonCommand:  
  10.         {  
  11.             int delta = (option->state & State_MouseOver) ? 64 : 0;  
  12.             QColor slightlyOpaqueBlack(0, 0, 0, 63);  
  13.             QColor semiTransparentWhite(255, 255, 255, 127 + delta);  
  14.             QColor semiTransparentBlack(0, 0, 0, 127 - delta);  
  15.   
  16.             int x, y, width, height;  
  17.             option->rect.getRect(&x, &y, &width, &height);  
  18. //! [12]  
  19.   
  20. //! [13]  
  21.             QPainterPath roundRect = roundRectPath(option->rect);  
  22. //! [13] //! [14]  
  23.             int radius = qMin(width, height) / 2;  
  24. //! [14]  
  25.   
  26. //! [15]  
  27.             QBrush brush;  
  28. //! [15] //! [16]  
  29.             bool darker;  
  30.   
  31.             const QStyleOptionButton *buttonOption =  
  32.                     qstyleoption_cast<const QStyleOptionButton *>(option);  
  33.             if (buttonOption  
  34.                     && (buttonOption->features & QStyleOptionButton::Flat)) {  
  35.                 brush = option->palette.background();  
  36.                 darker = (option->state & (State_Sunken | State_On));  
  37.             } else {  
  38.                 if (option->state & (State_Sunken | State_On)) {  
  39.                     brush = option->palette.mid();  
  40.                     darker = !(option->state & State_Sunken);  
  41.                 } else {  
  42.                     brush = option->palette.button();  
  43.                     darker = false;  
  44. //! [16] //! [17]  
  45.                 }  
  46. //! [17] //! [18]  
  47.             }  
  48. //! [18]  
  49.   
  50. //! [19]  
  51.             painter->save();  
  52. //! [19] //! [20]  
  53.             painter->setRenderHint(QPainter::Antialiasing, true);  
  54. //! [20] //! [21]  
  55.             painter->fillPath(roundRect, brush);  
  56. //! [21] //! [22]  
  57.             if (darker)  
  58. //! [22] //! [23]  
  59.                 painter->fillPath(roundRect, slightlyOpaqueBlack);  
  60. //! [23]  
  61.   
  62. //! [24]  
  63.             int penWidth;  
  64. //! [24] //! [25]  
  65.             if (radius < 10)  
  66.                 penWidth = 3;  
  67.             else if (radius < 20)  
  68.                 penWidth = 5;  
  69.             else  
  70.                 penWidth = 7;  
  71.   
  72.             QPen topPen(semiTransparentWhite, penWidth);  
  73.             QPen bottomPen(semiTransparentBlack, penWidth);  
  74.   
  75.             if (option->state & (State_Sunken | State_On))  
  76.                 qSwap(topPen, bottomPen);  
  77. //! [25]  
  78.   
  79. //! [26]  
  80.             int x1 = x;  
  81.             int x2 = x + radius;  
  82.             int x3 = x + width - radius;  
  83.             int x4 = x + width;  
  84.   
  85.             if (option->direction == Qt::RightToLeft) {  
  86.                 qSwap(x1, x4);  
  87.                 qSwap(x2, x3);  
  88.             }  
  89.   
  90.             QPolygon topHalf;  
  91.             topHalf << QPoint(x1, y)  
  92.                     << QPoint(x4, y)  
  93.                     << QPoint(x3, y + radius)  
  94.                     << QPoint(x2, y + height - radius)  
  95.                     << QPoint(x1, y + height);  
  96.   
  97.             painter->setClipPath(roundRect);  
  98.             painter->setClipRegion(topHalf, Qt::IntersectClip);  
  99.             painter->setPen(topPen);  
  100.             painter->drawPath(roundRect);  
  101. //! [26] //! [32]  
  102.   
  103.             QPolygon bottomHalf = topHalf;  
  104.             bottomHalf[0] = QPoint(x4, y + height);  
  105.   
  106.             painter->setClipPath(roundRect);  
  107.             painter->setClipRegion(bottomHalf, Qt::IntersectClip);  
  108.             painter->setPen(bottomPen);  
  109.             painter->drawPath(roundRect);  
  110.   
  111.             painter->setPen(option->palette.foreground().color());  
  112.             painter->setClipping(false);  
  113.             painter->drawPath(roundRect);  
  114.   
  115.             painter->restore();  
  116.         }  
  117.         break;  
  118. //! [32] //! [33]  
  119.     default:  
  120. //! [33] //! [34]  
  121.         QMotifStyle::drawPrimitive(element, option, painter, widget);  
  122.     }  
  123. }  
  124. //! [34]  


(6)void drawControl(ControlElement control, const QStyleOption *option,
                     QPainter *painter, const QWidget *widget) const

      功能:和上个函数类似。具体参见Qt帮组文档。

      具体实现:

  1. //! [35] 
  2. void NorwegianWoodStyle::drawControl(ControlElement
    element, 
  3. //! [35] //! [36] 
  4.                                      const QStyleOption
    *option, 
  5.                                      QPainter *painter, 
  6.                                      const QWidget
    *widget) 
    const 
  7.     switch (element)
  8.     case CE_PushButtonLabel: 
  9.         { 
  10.             QStyleOptionButton myButtonOption; 
  11.             const QStyleOptionButton
    *buttonOption = 
  12.                     qstyleoption_cast<const QStyleOptionButton
    *>(option); 
  13.             if (buttonOption)
  14.                 myButtonOption = *buttonOption; 
  15.                 if (myButtonOption.palette.currentColorGroup() 
  16.                         != QPalette::Disabled) { 
  17.                     if (myButtonOption.state
    & (State_Sunken | State_On)) { 
  18.                         myButtonOption.palette.setBrush(QPalette::ButtonText, 
  19.                                 myButtonOption.palette.brightText()); 
  20.                     } 
  21.                 } 
  22.             } 
  23.             QMotifStyle::drawControl(element, &myButtonOption, painter, widget); 
  24.         } 
  25.         break
  26.     default
  27.         QMotifStyle::drawControl(element, option, painter, widget); 
  28.     } 
  29. //! [36] 
  1. //! [35]  
  2. void NorwegianWoodStyle::drawControl(ControlElement element,  
  3. //! [35] //! [36]  
  4.                                      const QStyleOption *option,  
  5.                                      QPainter *painter,  
  6.                                      const QWidget *widget) const  
  7. {  
  8.     switch (element) {  
  9.     case CE_PushButtonLabel:  
  10.         {  
  11.             QStyleOptionButton myButtonOption;  
  12.             const QStyleOptionButton *buttonOption =  
  13.                     qstyleoption_cast<const QStyleOptionButton *>(option);  
  14.             if (buttonOption) {  
  15.                 myButtonOption = *buttonOption;  
  16.                 if (myButtonOption.palette.currentColorGroup()  
  17.                         != QPalette::Disabled) {  
  18.                     if (myButtonOption.state & (State_Sunken | State_On)) {  
  19.                         myButtonOption.palette.setBrush(QPalette::ButtonText,  
  20.                                 myButtonOption.palette.brightText());  
  21.                     }  
  22.                 }  
  23.             }  
  24.             QMotifStyle::drawControl(element, &myButtonOption, painter, widget);  
  25.         }  
  26.         break;  
  27.     default:  
  28.         QMotifStyle::drawControl(element, option, painter, widget);  
  29.     }  
  30. }  
  31. //! [36]  


(7)两个辅助函数,这里代码比较好懂,直接贴代码了:

  1. //! [37] 
  2. void NorwegianWoodStyle::setTexture(QPalette
    &palette, QPalette::ColorRole role, 
  3. //! [37] //! [38] 
  4.                                     const QPixmap
    &pixmap) 
  5.     for (int i
    = 0; i < QPalette::NColorGroups; ++i) { 
  6.         QColor color = palette.brush(QPalette::ColorGroup(i), role).color(); 
  7.         palette.setBrush(QPalette::ColorGroup(i), role, QBrush(color, pixmap)); 
  8.     } 
  9. //! [38] 
  10.  
  11. //! [39] 
  12. QPainterPath NorwegianWoodStyle::roundRectPath(const QRect
    &rect) 
  13. //! [39] //! [40] 
  14.     int radius
    = qMin(rect.width(), rect.height()) / 2; 
  15.     int diam
    = 2 * radius; 
  16.  
  17.     int x1,
    y1, x2, y2; 
  18.     rect.getCoords(&x1, &y1, &x2, &y2); 
  19.  
  20.     QPainterPath path; 
  21.     path.moveTo(x2, y1 + radius); 
  22.     path.arcTo(QRect(x2 - diam, y1, diam, diam), 0.0, +90.0); 
  23.     path.lineTo(x1 + radius, y1); 
  24.     path.arcTo(QRect(x1, y1, diam, diam), 90.0, +90.0); 
  25.     path.lineTo(x1, y2 - radius); 
  26.     path.arcTo(QRect(x1, y2 - diam, diam, diam), 180.0, +90.0); 
  27.     path.lineTo(x1 + radius, y2); 
  28.     path.arcTo(QRect(x2 - diam, y2 - diam, diam, diam), 270.0, +90.0); 
  29.     path.closeSubpath(); 
  30.     return path; 
  31. //! [40] 
  1. //! [37]  
  2. void NorwegianWoodStyle::setTexture(QPalette &palette, QPalette::ColorRole role,  
  3. //! [37] //! [38]  
  4.                                     const QPixmap &pixmap)  
  5. {  
  6.     for (int i = 0; i < QPalette::NColorGroups; ++i) {  
  7.         QColor color = palette.brush(QPalette::ColorGroup(i), role).color();  
  8.         palette.setBrush(QPalette::ColorGroup(i), role, QBrush(color, pixmap));  
  9.     }  
  10. }  
  11. //! [38]  
  12.   
  13. //! [39]  
  14. QPainterPath NorwegianWoodStyle::roundRectPath(const QRect &rect)  
  15. //! [39] //! [40]  
  16. {  
  17.     int radius = qMin(rect.width(), rect.height()) / 2;  
  18.     int diam = 2 * radius;  
  19.   
  20.     int x1, y1, x2, y2;  
  21.     rect.getCoords(&x1, &y1, &x2, &y2);  
  22.   
  23.     QPainterPath path;  
  24.     path.moveTo(x2, y1 + radius);  
  25.     path.arcTo(QRect(x2 - diam, y1, diam, diam), 0.0, +90.0);  
  26.     path.lineTo(x1 + radius, y1);  
  27.     path.arcTo(QRect(x1, y1, diam, diam), 90.0, +90.0);  
  28.     path.lineTo(x1, y2 - radius);  
  29.     path.arcTo(QRect(x1, y2 - diam, diam, diam), 180.0, +90.0);  
  30.     path.lineTo(x1 + radius, y2);  
  31.     path.arcTo(QRect(x2 - diam, y2 - diam, diam, diam), 270.0, +90.0);  
  32.     path.closeSubpath();  
  33.     return path;  
  34. }  
  35. //! [40]  

【上篇】
【下篇】

抱歉!评论已关闭.