📄 qpainter.cpp
字号:
bool conicalGradient = false; bool patternBrush = false; bool xform = false; bool complexXform = false; bool skip = true; // Pen and brush properties (we have to check both if one changes because the // one that's unchanged can still be in a state which requires emulation) if (s->state() & QPaintEngine::DirtyPen || s->state() & QPaintEngine::DirtyBrush) { // Check Brush stroke emulation if (!s->pen.isSolid() && !engine->hasFeature(QPaintEngine::BrushStroke)) s->emulationSpecifier |= QPaintEngine::BrushStroke; else s->emulationSpecifier &= ~QPaintEngine::BrushStroke; skip = false; QBrush penBrush = s->pen.brush(); Qt::BrushStyle brushStyle = s->brush.style(); Qt::BrushStyle penBrushStyle = penBrush.style(); alpha = (penBrushStyle != Qt::NoBrush && (penBrushStyle < Qt::LinearGradientPattern && penBrush.color().alpha() != 255) && !penBrush.isOpaque()) || (brushStyle != Qt::NoBrush && (brushStyle < Qt::LinearGradientPattern && s->brush.color().alpha() != 255) && !s->brush.isOpaque()); linearGradient = ((penBrushStyle == Qt::LinearGradientPattern) || (brushStyle == Qt::LinearGradientPattern)); radialGradient = ((penBrushStyle == Qt::RadialGradientPattern) || (brushStyle == Qt::RadialGradientPattern)); conicalGradient = ((penBrushStyle == Qt::ConicalGradientPattern) || (brushStyle == Qt::ConicalGradientPattern)); patternBrush = (((penBrushStyle > Qt::SolidPattern && penBrushStyle < Qt::LinearGradientPattern) || penBrushStyle == Qt::TexturePattern) || ((brushStyle > Qt::SolidPattern && brushStyle < Qt::LinearGradientPattern) || brushStyle == Qt::TexturePattern)); if (((penBrush.style() == Qt::TexturePattern && penBrush.texture().hasAlpha()) || (s->brush.style() == Qt::TexturePattern && s->brush.texture().hasAlpha())) && !engine->hasFeature(QPaintEngine::MaskedBrush)) s->emulationSpecifier |= QPaintEngine::MaskedBrush; else s->emulationSpecifier &= ~QPaintEngine::MaskedBrush; } if (s->state() & (QPaintEngine::DirtyHints | QPaintEngine::DirtyOpacity | QPaintEngine::DirtyBackgroundMode)) { skip = false; } if (skip) return;#if 0 qDebug("QPainterPrivate::updateEmulationSpecifier, state=%p\n" " - alpha: %d\n" " - linearGradient: %d\n" " - radialGradient: %d\n" " - conicalGradient: %d\n" " - patternBrush: %d\n" " - hints: %x\n" " - xform: %d\n", s, alpha, linearGradient, radialGradient, conicalGradient, patternBrush, uint(s->renderHints), xform);#endif // XForm properties if (s->state() & QPaintEngine::DirtyTransform) { xform = !s->matrix.isIdentity(); complexXform = !s->matrix.isAffine(); } else if (s->txop >= QTransform::TxTranslate) { xform = true; complexXform = !s->matrix.isAffine(); } const bool brushXform = (!s->brush.transform().type() == QTransform::TxNone); const bool penXform = (!s->pen.brush().transform().type() == QTransform::TxNone); const bool patternXform = patternBrush && (xform || brushXform || penXform); // Check alphablending if (alpha && !engine->hasFeature(QPaintEngine::AlphaBlend)) s->emulationSpecifier |= QPaintEngine::AlphaBlend; else s->emulationSpecifier &= ~QPaintEngine::AlphaBlend; // Linear gradient emulation if (linearGradient && !engine->hasFeature(QPaintEngine::LinearGradientFill)) s->emulationSpecifier |= QPaintEngine::LinearGradientFill; else s->emulationSpecifier &= ~QPaintEngine::LinearGradientFill; // Radial gradient emulation if (radialGradient && !engine->hasFeature(QPaintEngine::RadialGradientFill)) s->emulationSpecifier |= QPaintEngine::RadialGradientFill; else s->emulationSpecifier &= ~QPaintEngine::RadialGradientFill; // Conical gradient emulation if (conicalGradient && !engine->hasFeature(QPaintEngine::ConicalGradientFill)) s->emulationSpecifier |= QPaintEngine::ConicalGradientFill; else s->emulationSpecifier &= ~QPaintEngine::ConicalGradientFill; // Pattern brushes if (patternBrush && !engine->hasFeature(QPaintEngine::PatternBrush)) s->emulationSpecifier |= QPaintEngine::PatternBrush; else s->emulationSpecifier &= ~QPaintEngine::PatternBrush; // Pattern XForms if (patternXform && !engine->hasFeature(QPaintEngine::PatternTransform)) s->emulationSpecifier |= QPaintEngine::PatternTransform; else s->emulationSpecifier &= ~QPaintEngine::PatternTransform; // Primitive XForms if (xform && !engine->hasFeature(QPaintEngine::PrimitiveTransform)) s->emulationSpecifier |= QPaintEngine::PrimitiveTransform; else s->emulationSpecifier &= ~QPaintEngine::PrimitiveTransform; // Perspective XForms if (complexXform && !engine->hasFeature(QPaintEngine::PerspectiveTransform)) s->emulationSpecifier |= QPaintEngine::PerspectiveTransform; else s->emulationSpecifier &= ~QPaintEngine::PerspectiveTransform; // Constant opacity if (state->opacity != 1 && !engine->hasFeature(QPaintEngine::ConstantOpacity)) s->emulationSpecifier |= QPaintEngine::ConstantOpacity; else s->emulationSpecifier &= ~QPaintEngine::ConstantOpacity; bool gradientStretch = false; if (linearGradient || conicalGradient || radialGradient) { gradientStretch |= check_gradient(s->brush); gradientStretch |= check_gradient(s->pen.brush()); } if (gradientStretch) s->emulationSpecifier |= QGradient_StretchToDevice; else s->emulationSpecifier &= ~QGradient_StretchToDevice; // Opaque backgrounds... if (s->bgMode == Qt::OpaqueMode && (is_pen_transparent(s->pen) || is_brush_transparent(s->brush))) s->emulationSpecifier |= QPaintEngine_OpaqueBackground; else s->emulationSpecifier &= ~QPaintEngine_OpaqueBackground;#if 0 //won't be correct either way because the device can already have // something rendered to it in which case subsequent emulation // on a fully transparent qimage and then blitting the results // won't produce correct results // Blend modes if (state->composition_mode > QPainter::CompositionMode_Xor && !engine->hasFeature(QPaintEngine::BlendModes)) s->emulationSpecifier |= QPaintEngine::BlendModes; else s->emulationSpecifier &= ~QPaintEngine::BlendModes;#endif}void QPainterPrivate::updateState(QPainterState *newState){ if (!newState) { engine->state = newState; } else if (newState->state() || engine->state!=newState) { // ### we might have to call QPainter::begin() here... if (!engine->state) { engine->state = newState; engine->setDirty(QPaintEngine::AllDirty); } if (engine->state->painter() != newState->painter) // ### this could break with clip regions vs paths. engine->setDirty(QPaintEngine::AllDirty); // Upon restore, revert all changes since last save else if (engine->state != newState) newState->dirtyFlags |= QPaintEngine::DirtyFlags(static_cast<QPainterState *>(engine->state)->changeFlags); // We need to store all changes made so that restore can deal with them else newState->changeFlags |= newState->dirtyFlags; updateEmulationSpecifier(newState); // Unset potential dirty background mode newState->dirtyFlags &= ~(QPaintEngine::DirtyBackgroundMode | QPaintEngine::DirtyBackground); engine->state = newState; engine->updateState(*newState); engine->clearDirty(QPaintEngine::AllDirty); }}/*! \class QPainter \brief The QPainter class performs low-level painting on widgets and other paint devices. \ingroup multimedia \mainclass QPainter provides highly optimized functions to do most of the drawing GUI programs require. It can draw everything from simple lines to complex shapes like pies and chords. It can also draw aligned text and pixmaps. Normally, it draws in a "natural" coordinate system, but it can also do view and world transformation. QPainter can operate on any object that inherits the QPaintDevice class. The common use of QPainter is inside a widget's paint event: Construct and customize (e.g. set the pen or the brush) the painter. Then draw. Remember to destroy the QPainter object after drawing. For example: \code void SimpleExampleWidget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setPen(Qt::blue); painter.setFont(QFont("Arial", 30)); painter.drawText(rect(), Qt::AlignCenter, "Qt"); } \endcode The core functionality of QPainter is drawing, but the class also provide several functions that allows you to customize QPainter's settings and its rendering quality, and others that enable clipping. In addition you can control how different shapes are merged together by specifying the painter's composition mode. The isActive() function indicates whether the painter is active. A painter is activated by the begin() function and the constructor that takes a QPaintDevice argument. The end() function, and the destructor, deactivates it. Together with the QPaintDevice and QPaintEngine classes, QPainter form the basis for Qt's paint system. QPainter is the class used to perform drawing operations. QPaintDevice represents a device that can be painted on using a QPainter. QPaintEngine provides the interface that the painter uses to draw onto different types of devices. If the painter is active, device() returns the paint device on which the painter paints, and paintEngine() returns the paint engine that the painter is currently operating on. For more information, see \l {The Paint System} documentation. Sometimes it is desirable to make someone else paint on an unusual QPaintDevice. QPainter supports a static function to do this, setRedirected(). \warning When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting. \tableofcontents \section1 Settings There are several settings that you can customize to make QPainter draw according to your preferences: \list \o font() is the font used for drawing text. If the painter isActive(), you can retrieve information about the currently set font, and its metrics, using the fontInfo() and fontMetrics() functions respectively. \o brush() defines the color or pattern that is used for filling shapes. \o pen() defines the color or stipple that is used for drawing lines or boundaries. \o backgroundMode() defines whether there is a background() or not, i.e it is either Qt::OpaqueMode or Qt::TransparentMode. \o background() only applies when backgroundMode() is \l Qt::OpaqueMode and pen() is a stipple. In that case, it describes the color of the background pixels in the stipple. \o brushOrigin() defines the origin of the tiled brushes, normally the origin of widget's background. \o viewport(), window(), worldMatrix() make up the painter's coordinate transformation system. For more information, see the \l {Coordinate Transformations} section and the \l {The Coordinate System} documentation. \o hasClipping() tells whether the painter clips at all. (The paint device clips, too.) If the painter clips, it clips to clipRegion(). \o layoutDirection() defines the layout direction used by the painter when drawing text. \o matrixEnabled() tells whether world transformation is enabled. \o viewTransformEnabled() tells whether view transformation is enabled. \endlist Note that some of these settings mirror settings in some paint devices, e.g. QWidget::font(). The QPainter::begin() function (or equivalently the QPainter constructor) copies these attributes from the paint device. You can at any time save the QPainter's state by calling the save() function which saves all the available settings on an internal stack. The restore() function pops them back. \section1 Drawing QPainter provides functions to draw most primitives: drawPoint(), drawPoints(), drawLine(), drawRect(), drawRoundRect(), drawEllipse(), drawArc(), drawPie(), drawChord(), drawPolyline(), drawPolygon(), drawConvexPolygon() and drawCubicBezier(). The two convenience functions, drawRects() and drawLines(), draw the given number of rectangles or lines in the given array of \l {QRect}{QRects} or \l {QLine}{QLines} using the current pen and brush. The QPainter class also provides the fillRect() function which fills the given QRect, with the given QBrush, and the eraseRect() function that erases the area inside the given rectangle. All of these functions have both integer and floating point versions. \table 100% \row \o \inlineimage qpainter-basicdrawing.png \o \bold {Basic Drawing Example} The \l {painting/basicdrawing}{Basic Drawing} example shows how to display basic graphics primitives in a variety of styles using the QPainter class. \endtable If you need to draw a complex shape, especially if you need to do so repeatedly, consider creating a QPainterPath and drawing it using drawPath(). \table 100% \row \o \bold {Painter Paths example} The QPainterPath class provides a container for painting operations, enabling graphical shapes to be constructed and reused. The \l {painting/painterpaths}{Painter Paths} example shows how painter paths can be used to build complex shapes for rendering. \o \inlineimage qpainter-painterpaths.png \endtable QPainter also provides the fillPath() function which fills the given QPainterPath with the given QBrush, and the strokePath()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -