📄 qstyle.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qstyle.h"#include "qapplication.h"#include "qpainter.h"#include "qwidget.h"#include "qbitmap.h"#include "qpixmapcache.h"#include "qstyleoption.h"#include "private/qstyle_p.h"#ifndef QT_NO_DEBUG#include "qdebug.h"#endif#ifdef Q_WS_X11#include <qx11info_x11.h>#endif#include <limits.h>static const int MaxBits = 8 * sizeof(QSizePolicy::ControlType);static int unpackControlTypes(QSizePolicy::ControlTypes controls, QSizePolicy::ControlType *array){ if (!controls) return 0; // optimization: exactly one bit is set if ((controls & (controls - 1)) == 0) { array[0] = QSizePolicy::ControlType(uint(controls)); return 1; } int count = 0; for (int i = 0; i < MaxBits; ++i) { if (uint bit = (controls & (0x1 << i))) array[count++] = QSizePolicy::ControlType(bit); } return count;}/*! \class QStyle \brief The QStyle class is an abstract base class that encapsulates the look and feel of a GUI. \ingroup appearance Qt contains a set of QStyle subclasses that emulate the styles of the different platforms supported by Qt (QWindowsStyle, QMacStyle, QMotifStyle, etc.). By default, these styles are built into the QtGui library. Styles can also be made available as plugins. Qt's built-in widgets use QStyle to perform nearly all of their drawing, ensuring that they look exactly like the equivalent native widgets. The diagram below shows a QComboBox in six different styles. \img qstyle-comboboxes.png Six combo boxes Topics: \tableofcontents \section1 Setting a Style The style of the entire application can be set using the QApplication::setStyle() function. It can also be specified by the user of the application, using the \c -style command-line option: \code ./myapplication -style motif \endcode If no style is specified, Qt will choose the most appropriate style for the user's platform or desktop environment. A style can also be set on an individual widget using the QWidget::setStyle() function. \section1 Developing Style-Aware Custom Widgets If you are developing custom widgets and want them to look good on all platforms, you can use QStyle functions to perform parts of the widget drawing, such as drawItemText(), drawItemPixmap(), drawPrimitive(), drawControl(), and drawComplexControl(). Most QStyle draw functions take four arguments: \list \o an enum value specifying which graphical element to draw \o a QStyleOption specifying how and where to render that element \o a QPainter that should be used to draw the element \o a QWidget on which the drawing is performed (optional) \endlist For example, if you want to draw a focus rectangle on your widget, you can write: \quotefromfile snippets/styles/styles.cpp \skipto MyWidget::paintEvent \printuntil } QStyle gets all the information it needs to render the graphical element from QStyleOption. 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, you can use QStyle to draw on any paint device, not just widgets, by setting the QPainter properly. QStyleOption has various subclasses for the various types of graphical elements that can be drawn. For example, PE_FrameFocusRect expects a QStyleOptionFocusRect argument. To ensure that drawing operations are as fast as possible, QStyleOption and its subclasses have public data members. See the QStyleOption class documentation for details on how to use it. For convenience, Qt provides the QStylePainter class, which combines a QStyle, a QPainter, and a QWidget. This makes it possible to write \skipto QStylePainter painter \printline painter \dots \skipto drawPrimitive \printline drawPrimitive instead of \quotefromfile snippets/styles/styles.cpp \skipto QPainter painter \printline painter \dots \skipto drawPrimitive \printline drawPrimitive \section1 Creating a Custom Style If you want to design a custom look and feel for your application, the first step is to pick one of the base styles provided with Qt to build your custom style from. The choice will depend on which existing style resembles your style the most. Depending on which parts of the base style you want to change, you must reimplement the functions that are used to draw those parts of the interface. To illustrate this, we will modify the look of the spin box arrows drawn by QWindowsStyle. The arrows are \e{primitive elements} that are drawn by the drawPrimitive() function, so we need to reimplement that function. We need the following class declaration: \quotefile snippets/customstyle/customstyle.h \skipto class CustomStyle \printuntil }; To draw its up and down arrows, QSpinBox uses the PE_IndicatorSpinUp and PE_IndicatorSpinDown primitive elements. Here's how to reimplement the drawPrimitive() function to draw them differently: \quotefile snippets/customstyle/customstyle.cpp \skipto CustomStyle::drawPrimitive \printuntil QWindowsStyle::drawPrimitive \printline } \printline } Notice that we don't use the \c widget argument, except to pass it on to the QWindowStyle::drawPrimitive() function. As mentioned earlier, the information about what is to be drawn and how it should be drawn is specified by a QStyleOption object, so there is no need to ask the widget. If you need to use the \c widget argument to obtain additional information, be careful to ensure that it isn't 0 and that it is of the correct type before using it. For example: \quotefile snippets/customstyle/customstyle.cpp \skipto CustomStyle::CustomStyle \skipto QSpinBox \printuntil { \dots \printuntil } When implementing a custom style, you cannot assume that the widget is a QSpinBox just because the enum value is called PE_IndicatorSpinUp or PE_IndicatorSpinDown. The documentation for the \l{widgets/styles}{Styles} example covers this topic in more detail. \section1 Using a Custom Style There are several ways of using a custom style in a Qt application. The simplest way is call the QApplication::setStyle() static function before creating the QApplication object: \include snippets/customstyle/main.cpp You can call QApplication::setStyle() at any time, but by calling it before the constructor, you ensure that the user's preference, set using the \c -style command-line option, is respected. You may want to make your style available for use in other applications, some of which may not be yours and are not available for you to recompile. The Qt Plugin system makes it possible to create styles as plugins. Styles created as plugins are loaded as shared objects at runtime by Qt itself. Please refer to the \link plugins-howto.html Qt Plugin\endlink documentation for more information on how to go about creating a style plugin. Compile your plugin and put it into Qt's \c plugins/styles directory. We now have a pluggable style that Qt can load automatically. To use your new style with existing applications, simply start the application with the following argument: \code ./myapplication -style custom \endcode The application will use the look and feel from the custom style you implemented. \section1 Right-to-Left Desktops Languages written from right to left (such as Arabic and Hebrew) usually also mirror the whole layout of widgets, and require the light to come from the screen's top-right corner instead of top-left. If you create a custom style, you should take special care when drawing asymmetric elements to make sure that they also look correct in a mirrored layout. An easy way to test your styles is to run applications with the \c -reverse command-line option or to call QApplication::setLayoutDirection() in your \c main() function. Here are some things to keep in mind when making a style work well in a right-to-left environment: \list \o subControlRect() and subElementRect() return rectangles in screen coordinates \o QStyleOption::direction indicates in which direction the item should be drawn in \o If a style is not right-to-left aware it will display items as if it were left-to-right \o visualRect(), visualPos(), and visualAlignment() are helpful functions that will translate from logical to screen representations. \o alignedRect() will return a logical rect aligned for the current direction \endlist \sa QStyleOption, QStylePainter, {Styles Example}, {Implementing Styles and Style Aware Widgets}*//*! Constructs a style object.*/QStyle::QStyle() : QObject(*new QStylePrivate){}/*! \internal Constructs a style object.*/QStyle::QStyle(QStylePrivate &dd) : QObject(dd){}/*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -