Sophie

Sophie

distrib > * > 2009.0 > i586 > by-pkgid > a6711891ce757817bba854bf3f25205a > files > 2283

qtjambi-doc-4.3.3-3mdv2008.1.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /home/gvatteka/dev/qt-4.3/doc/src/qt4-styles.qdoc -->
<head>
  <title>The Qt 4 Style API</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 align="center">The Qt 4 Style API<br /><small></small></h1>
<p>Qt's style API is responsible for performing the widget drawing for built-in widgets. The Qt 4 style API has been revised to make it possible for a style to draw widgets without calling any functions on the widget.</p>
<p>Because Qt 4 is split across multiple libraries, Qt needed this update to be able to draw widgets from other libraries than QtGui. For application developers, this has other benefits, such as more managable parameter lists and the possibility of drawing any graphical element without having a widget of a specific type.</p>
<a name="general-overview"></a>
<h2>General Overview</h2>
<p>The <a href="gui/QStyle.html"><tt>QStyle</tt></a> class is an abstract base class that encapsulates the look and feel of a GUI. Qt's built-in widgets use it to perform nearly all of their drawing, ensuring that they look exactly like the equivalent native widgets.</p>
<p>Most draw functions now take four arguments:</p>
<ul>
<li>an enum value specifying which graphical element to draw</li>
<li>a <a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a> specifying how and where to render that element</li>
<li>a <a href="gui/QPainter.html"><tt>QPainter</tt></a> that should be used to draw the element</li>
<li>a <a href="gui/QWidget.html"><tt>QWidget</tt></a> on which the drawing is performed (optional)</li>
</ul>
<p>The style gets all the information it needs to render the graphical element from <a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a>. The widget is passed as the last argument in case the style needs it to perform special effects (such as animated default buttons on Mac OS X), but it isn't mandatory. In fact, <a href="gui/QStyle.html"><tt>QStyle</tt></a> can be used to draw on any paint device, not just widgets, by setting the <a href="gui/QPainter.html"><tt>QPainter</tt></a> properly.</p>
<p>Thanks to <a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a>, it is now possible to make <a href="gui/QStyle.html"><tt>QStyle</tt></a> draw widgets without linking in any code for the widget. This is how Qt's built-in styles can draw Qt 3 widgets such as Q3ListView without necessarily linking against the Qt3Support library. Another significant benefit of the new approach is that it's now possible to use <a href="gui/QStyle.html"><tt>QStyle</tt></a>'s draw functions on other widgets than the built-in widgets; for example, you can draw a combobox on any widget, not just on a <a href="gui/QComboBox.html"><tt>QComboBox</tt></a>.</p>
<p><a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a> has various subclasses for the various types of graphical elements that can be drawn, and it's possible to create custom subclasses. For example, the QStyle::PE_FrameFocusRect element expects a <a href="gui/QStyleOptionFocusRect.html"><tt>QStyleOptionFocusRect</tt></a> argument. This is documented for each enum value.</p>
<p>When reimplementing <a href="gui/QStyle.html"><tt>QStyle</tt></a> functions that take a <a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a> parameter, you often need to cast the <a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a> to a subclass (e.g&#x2e;, <a href="gui/QStyleOptionFocusRect.html"><tt>QStyleOptionFocusRect</tt></a>). For safety, you can use qstyleoption_cast() to ensure that the pointer type is correct. If the object isn't of the right type, qstyleoption_cast() returns 0. For example:</p>
<pre>    const QStyleOptionFocusRect *focusRectOption =
            qstyleoption_cast&lt;const QStyleOptionFocusRect *&gt;(option);
    if (focusRectOption) {
        ...
    }</pre>
<p>For performance reasons, there are few member functions and the access to the variables is direct. This &quot;low-level&quot; feel makes the structures use straightforward and emphasizes that these are simply parameters used by the style functions. In addition, the caller of a <a href="gui/QStyle.html"><tt>QStyle</tt></a> function usually creates <a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a> objects on the stack. This combined with Qt's extensive use of implicit sharing</tt> for types such as <a href="porting4.html#qstring"><tt>QString</tt></a>, <a href="gui/QPalette.html"><tt>QPalette</tt></a>, and <a href="gui/QColor.html"><tt>QColor</tt></a> ensures that no memory allocation needlessly takes place. (Dynamic memory allocation can be an expensive operation, especially when drawing very often in a short time.)</p>
<a name="example-code"></a>
<h2>Example Code</h2>
<p>The following code snippet illustrates how to use <a href="gui/QStyle.html"><tt>QStyle</tt></a> to draw the focus rectangle from a custom widget's paintEvent():</p>
<pre>    void MyWidget::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        ...

        QStyleOptionFocusRect option(1);
        option.init(this);
        option.backgroundColor = palette().color(QPalette::Window);

        style().drawPrimitive(QStyle::PE_FrameFocusRect, &amp;option, &amp;painter,
                              this);
    }</pre>
<p>The next example shows how to derive from an existing style to customize the look of a graphical element:</p>
<pre>    class CustomStyle : public QWindowsStyle
    {
        Q_OBJECT

    public:
        CustomStyle()
        ~CustomStyle() {}

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

    void CustomStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option,
                                    QPainter *painter, const QWidget *widget) const
    {
        if (element == PE_IndicatorSpinUp || element == PE_IndicatorSpinDown) {
            QPolygon points(3);
            int x = option-&gt;rect.x();
            int y = option-&gt;rect.y();
            int w = option-&gt;rect.width() / 2;
            int h = option-&gt;rect.height() / 2;
            x += (option-&gt;rect.width() - w) / 2;
            y += (option-&gt;rect.height() - h) / 2;

            if (element == PE_IndicatorSpinUp) {
                points[0] = QPoint(x, y + h);
                points[1] = QPoint(x + w, y + h);
                points[2] = QPoint(x + w / 2, y);
            } else { <span class="comment">// PE_SpinBoxDown</span>
                points[0] = QPoint(x, y);
                points[1] = QPoint(x + w, y);
                points[2] = QPoint(x + w / 2, y + h);
            }

            if (option-&gt;state &amp; State_Enabled) {
                painter-&gt;setPen(option-&gt;palette.mid().color());
                painter-&gt;setBrush(option-&gt;palette.buttonText());
            } else {
                painter-&gt;setPen(option-&gt;palette.buttonText().color());
                painter-&gt;setBrush(option-&gt;palette.mid());
            }
            painter-&gt;drawPolygon(points);
        } else {
            QWindowsStyle::drawPrimitive(element, option, painter, widget);
        }
    }</pre>
<p>See also the Styles Example</tt> for a more detailed description of how custom styles can be created.</p>
<a name="comparison-with-qt-3"></a>
<h2>Comparison with Qt 3</h2>
<p>The <a href="gui/QStyle.html"><tt>QStyle</tt></a> class has a similar API in Qt 4 as in Qt 3, with more or less the same functions. What has changed is the signature of the functions and the role played by <a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a>. For example, here's the signature of the QStyle::drawControl() function in Qt 3:</p>
<pre>    void drawControl(ControlElement element,
                     QPainter *painter,
                     const QWidget *widget,
                     const QRect &amp;rect,
                     const QColorGroup &amp;colorGroup,
                     SFlags how = Style_Default,
                     const QStyleOption &amp;option = QStyleOption::Default) const;</pre>
<p>Here's the signature of the same function in Qt 4:</p>
<pre>    void drawControl(ControlElement element,
                     const QStyleOption *option,
                     QPainter *painter,
                     const QWidget *widget = 0) const;</pre>
<p>In Qt 3, some of the information required to draw a graphical element was stored in a <a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a> parameter, while the rest was deduced by querying the widget. In Qt 4, everything is stored in the <a href="gui/QStyleOption.html"><tt>QStyleOption</tt></a> parameter.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2007 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Jambi </div></td>
</tr></table></div></address></body>
</html>