解析Qt自帶的Style示例

本文轉自chenlong12580的專欄    點擊打開鏈接


在之前的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. {  
  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. };  
class NorwegianWoodStyle : public QMotifStyle
{
    Q_OBJECT

public:
    NorwegianWoodStyle() {}

    void polish(QPalette &palette);
    void polish(QWidget *widget);
    void unpolish(QWidget *widget);
    int pixelMetric(PixelMetric metric, const QStyleOption *option,
                    const QWidget *widget) const;
    int styleHint(StyleHint hint, const QStyleOption *option,
                  const QWidget *widget, QStyleHintReturn *returnData) const;
    void drawPrimitive(PrimitiveElement element, const QStyleOption *option,
                       QPainter *painter, const QWidget *widget) const;
    void drawControl(ControlElement control, const QStyleOption *option,
                     QPainter *painter, const QWidget *widget) const;

private:
    static void setTexture(QPalette &palette, QPalette::ColorRole role,
                           const QPixmap &pixmap);
    static QPainterPath roundRectPath(const QRect &rect);
};
        主要就是顯示了QMotifStyle類中的虛函數,而這些函數最初是QStyle類中的虛函數。下面介紹這幾個函數的功能:
(1)void polish(QPalette &palette)

     這是一個重載函數,功能就是改變調色板爲樣式指定的顏色調色板。

     該處具體實現爲:

  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]  
//! [0]
void NorwegianWoodStyle::polish(QPalette &palette)
{
    QColor brown(212, 140, 95);
    QColor beige(236, 182, 120);
    QColor slightlyOpaqueBlack(0, 0, 0, 63);

    QPixmap backgroundImage(":/images/woodbackground.png");
    QPixmap buttonImage(":/images/woodbutton.png");
    QPixmap midImage = buttonImage;

    QPainter painter;
    painter.begin(&midImage);
    painter.setPen(Qt::NoPen);
    painter.fillRect(midImage.rect(), slightlyOpaqueBlack);
    painter.end();
//! [0]

//! [1]
    palette = QPalette(brown);

    palette.setBrush(QPalette::BrightText, Qt::white);
    palette.setBrush(QPalette::Base, beige);
    palette.setBrush(QPalette::Highlight, Qt::darkGreen);
    setTexture(palette, QPalette::Button, buttonImage);
    setTexture(palette, QPalette::Mid, midImage);
    setTexture(palette, QPalette::Window, backgroundImage);

    QBrush brush = palette.background();
    brush.setColor(brush.color().dark());

    palette.setBrush(QPalette::Disabled, QPalette::WindowText, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Text, brush);
    palette.setBrush(QPalette::Disabled, QPalette::ButtonText, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Base, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Button, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Mid, brush);
}
//! [1]

(2)void polish(QWidget *widget)

       這是一個重載函數,功能是:初始化給定窗口部件的外觀,該函數在每一個窗口完全創建之後的某個時刻並且是該窗口部件每一次創建後首次顯示之前調用。注意,默認的實現是不做任何事情。該函數可能的動作就是調用QWidget::setBackgroundMode()。不要調用該函數來設置例如窗口部件顯示位置之類的信息。重現實現這個函數使得我們獲得課改變窗口部件顯示方式的後門,但是Qt的樣式引擎很少需要實現這個函數,而是重新實現 drawItemPixmap(), drawItemText(),drawPrimitive(),等等函數來代替。

       此處的實現爲:

  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]
void NorwegianWoodStyle::polish(QWidget *widget)
//! [3] //! [4]
{
    if (qobject_cast<QPushButton *>(widget)
            || qobject_cast<QComboBox *>(widget))
        widget->setAttribute(Qt::WA_Hover, true);
}
//! [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. {  
  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]  
//! [7]
int NorwegianWoodStyle::pixelMetric(PixelMetric metric,
//! [7] //! [8]
                                    const QStyleOption *option,
                                    const QWidget *widget) const
{
    switch (metric) {
    case PM_ComboBoxFrameWidth:
        return 8;
    case PM_ScrollBarExtent:
        return QMotifStyle::pixelMetric(metric, option, widget) + 4;
    default:
        return QMotifStyle::pixelMetric(metric, option, widget);
    }
}
//! [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. {  
  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]  
//! [9]
int NorwegianWoodStyle::styleHint(StyleHint hint, const QStyleOption *option,
//! [9] //! [10]
                                  const QWidget *widget,
                                  QStyleHintReturn *returnData) const
{
    switch (hint) {
    case SH_DitherDisabledText:
        return int(false);
    case SH_EtchDisabledText:
        return int(true);
    default:
        return QMotifStyle::styleHint(hint, option, widget, returnData);
    }
}
//! [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. {  
  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]  
//! [11]
void NorwegianWoodStyle::drawPrimitive(PrimitiveElement element,
//! [11] //! [12]
                                       const QStyleOption *option,
                                       QPainter *painter,
                                       const QWidget *widget) const
{
    switch (element) {
    case PE_PanelButtonCommand:
        {
            int delta = (option->state & State_MouseOver) ? 64 : 0;
            QColor slightlyOpaqueBlack(0, 0, 0, 63);
            QColor semiTransparentWhite(255, 255, 255, 127 + delta);
            QColor semiTransparentBlack(0, 0, 0, 127 - delta);

            int x, y, width, height;
            option->rect.getRect(&x, &y, &width, &height);
//! [12]

//! [13]
            QPainterPath roundRect = roundRectPath(option->rect);
//! [13] //! [14]
            int radius = qMin(width, height) / 2;
//! [14]

//! [15]
            QBrush brush;
//! [15] //! [16]
            bool darker;

            const QStyleOptionButton *buttonOption =
                    qstyleoption_cast<const QStyleOptionButton *>(option);
            if (buttonOption
                    && (buttonOption->features & QStyleOptionButton::Flat)) {
                brush = option->palette.background();
                darker = (option->state & (State_Sunken | State_On));
            } else {
                if (option->state & (State_Sunken | State_On)) {
                    brush = option->palette.mid();
                    darker = !(option->state & State_Sunken);
                } else {
                    brush = option->palette.button();
                    darker = false;
//! [16] //! [17]
                }
//! [17] //! [18]
            }
//! [18]

//! [19]
            painter->save();
//! [19] //! [20]
            painter->setRenderHint(QPainter::Antialiasing, true);
//! [20] //! [21]
            painter->fillPath(roundRect, brush);
//! [21] //! [22]
            if (darker)
//! [22] //! [23]
                painter->fillPath(roundRect, slightlyOpaqueBlack);
//! [23]

//! [24]
            int penWidth;
//! [24] //! [25]
            if (radius < 10)
                penWidth = 3;
            else if (radius < 20)
                penWidth = 5;
            else
                penWidth = 7;

            QPen topPen(semiTransparentWhite, penWidth);
            QPen bottomPen(semiTransparentBlack, penWidth);

            if (option->state & (State_Sunken | State_On))
                qSwap(topPen, bottomPen);
//! [25]

//! [26]
            int x1 = x;
            int x2 = x + radius;
            int x3 = x + width - radius;
            int x4 = x + width;

            if (option->direction == Qt::RightToLeft) {
                qSwap(x1, x4);
                qSwap(x2, x3);
            }

            QPolygon topHalf;
            topHalf << QPoint(x1, y)
                    << QPoint(x4, y)
                    << QPoint(x3, y + radius)
                    << QPoint(x2, y + height - radius)
                    << QPoint(x1, y + height);

            painter->setClipPath(roundRect);
            painter->setClipRegion(topHalf, Qt::IntersectClip);
            painter->setPen(topPen);
            painter->drawPath(roundRect);
//! [26] //! [32]

            QPolygon bottomHalf = topHalf;
            bottomHalf[0] = QPoint(x4, y + height);

            painter->setClipPath(roundRect);
            painter->setClipRegion(bottomHalf, Qt::IntersectClip);
            painter->setPen(bottomPen);
            painter->drawPath(roundRect);

            painter->setPen(option->palette.foreground().color());
            painter->setClipping(false);
            painter->drawPath(roundRect);

            painter->restore();
        }
        break;
//! [32] //! [33]
    default:
//! [33] //! [34]
        QMotifStyle::drawPrimitive(element, option, painter, widget);
    }
}
//! [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. {  
  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]  
//! [35]
void NorwegianWoodStyle::drawControl(ControlElement element,
//! [35] //! [36]
                                     const QStyleOption *option,
                                     QPainter *painter,
                                     const QWidget *widget) const
{
    switch (element) {
    case CE_PushButtonLabel:
        {
            QStyleOptionButton myButtonOption;
            const QStyleOptionButton *buttonOption =
                    qstyleoption_cast<const QStyleOptionButton *>(option);
            if (buttonOption) {
                myButtonOption = *buttonOption;
                if (myButtonOption.palette.currentColorGroup()
                        != QPalette::Disabled) {
                    if (myButtonOption.state & (State_Sunken | State_On)) {
                        myButtonOption.palette.setBrush(QPalette::ButtonText,
                                myButtonOption.palette.brightText());
                    }
                }
            }
            QMotifStyle::drawControl(element, &myButtonOption, painter, widget);
        }
        break;
    default:
        QMotifStyle::drawControl(element, option, painter, widget);
    }
}
//! [36]

(7)兩個輔助函數,這裏代碼比較好懂,直接貼代碼了:

  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] 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章