📄 qpainter.cpp
字号:
Note that while the brushOrigin() was necessary to adopt the parent's background for a widget in Qt 3, this is no longer the case since the Qt 4 painter doesn't paint the background unless you explicitly tell it to do so by setting the widget's \l {QWidget::autoFillBackground}{autoFillBackground} property to true. \sa brushOrigin(), {QPainter#Settings}{Settings}*/void QPainter::setBrushOrigin(const QPointF &p){ Q_D(QPainter);#ifdef QT_DEBUG_DRAW if (qt_show_painter_debug_output) printf("QPainter::setBrushOrigin(), (%.2f,%.2f)\n", p.x(), p.y());#endif d->state->bgOrigin = p; d->state->dirtyFlags |= QPaintEngine::DirtyBrushOrigin;}/*! \fn void QPainter::setBrushOrigin(const QPoint &position) \overload Sets the brush's origin to the given \a position.*//*! \fn void QPainter::setBrushOrigin(int x, int y) \overload Sets the brush's origin to point (\a x, \a y).*//*! \enum QPainter::CompositionMode Defines the Porter-Duff rules for digital image compositing. Composition modes are used to specify how the pixels in one image, the source, are merged with the pixel in another image, the destination. \image qpainter-compositionmode1.png \image qpainter-compositionmode2.png The most common type is SourceOver (often referred to as just alpha blending) where the source pixel is blended on top of the destination pixel in such a way that the alpha component of the source defines the translucency of the pixel. When the paint device is a QImage, the image format must be set to \l {QImage::Format}{Format_ARGB32Premultiplied} or \l {QImage::Format}{Format_ARGB32} for the composition modes to have any effect. For performance the premultiplied version is the preferred format. When a composition mode is set it applies to all painting operator, pens, brushes, gradients and pixmap/image drawing. \value CompositionMode_SourceOver This is the default mode. The alpha of the source is used to blend the pixel on top of the destination. \value CompositionMode_DestinationOver The alpha of the destination is used to blend it on top of the source pixels. This mode is the inverse of CompositionMode_SourceOver. \value CompositionMode_Clear The pixels in the destination are cleared (set to fully transparent) independent of the source. \value CompositionMode_Source The output is the source pixel. (This means a basic copy operation and is identical to SourceOver when the source pixel is opaque). \value CompositionMode_Destination The output is the destination pixel. This means that the blending has no effect. This mode is the inverse of CompositionMode_Source. \value CompositionMode_SourceIn The output is the source, where the alpha is reduced by that of the destination. \value CompositionMode_DestinationIn The output is the destination, where the alpha is reduced by that of the source. This mode is the inverse of CompositionMode_SourceIn. \value CompositionMode_SourceOut The output is the source, where the alpha is reduced by the inverse of destination. \value CompositionMode_DestinationOut The output is the destination, where the alpha is reduced by the inverse of the source. This mode is the inverse of CompositionMode_SourceOut. \value CompositionMode_SourceAtop The source pixel is blended on top of the destination, with the alpha of the source pixel reduced by the alpha of the destination pixel. \value CompositionMode_DestinationAtop The destination pixel is blended on top of the source, with the alpha of the destination pixel is reduced by the alpha of the destination pixel. This mode is the inverse of CompositionMode_SourceAtop. \value CompositionMode_Xor The source, whose alpha is reduced with the inverse of the destination alpha, is merged with the destination, whose alpha is reduced by the inverse of the source alpha. CompositionMode_Xor is not the same as the bitwise Xor. \value CompositionMode_Plus Both the alpha and color of the source and destination pixels are added together. \value CompositionMode_Multiply The output is the source color multiplied by the destination. Multiplying a color with white leaves the color unchanged, while multiplying a color with black produces black. \value CompositionMode_Screen The source and destination colors are inverted and then multiplied. Screening a color with white produces white, whereas screening a color with black leaves the color unchanged. \value CompositionMode_Overlay Multiplies or screens the colors depending on the destination color. The destination color is mixed with the source color to reflect the lightness or darkness of the destination. \value CompositionMode_Darken The darker of the source and destination colors is selected. \value CompositionMode_Lighten The lighter of the source and destination colors is selected. \value CompositionMode_ColorDodge The destination color is brightened to reflect the source color. A black source color leaves the destination color unchanged. \value CompositionMode_ColorBurn The destination color is darkened to reflect the source color. A white source color leaves the destination color unchanged. \value CompositionMode_HardLight Multiplies or screens the colors depending on the source color. A light source color will lighten the destination color, whereas a dark source color will darken the destination color. \value CompositionMode_SoftLight Darkens or lightens the colors depending on the source color. Similar to CompositionMode_HardLight. \value CompositionMode_Difference Subtracts the darker of the colors from the lighter. Painting with white inverts the destination color, whereas painting with black leaves the destination color unchanged. \value CompositionMode_Exclusion Similar to CompositionMode_Difference, but with a lower contrast. Painting with white inverts the destination color, whereas painting with black leaves the destination color unchanged. \sa compositionMode(), setCompositionMode(), {QPainter#Composition Modes}{Composition Modes}, {Image Composition Example}*//*! Sets the composition mode to the given \a mode. \warning You can only set the composition mode for QPainter objects that operates on a QImage. \sa compositionMode()*/void QPainter::setCompositionMode(CompositionMode mode){ Q_D(QPainter); if (!isActive()) { qWarning("QPainter::setCompositionMode: Painter not active"); return; } else if (!d->engine->hasFeature(QPaintEngine::PorterDuff)) { qWarning("QPainter::setCompositionMode: PorterDuff modes not supported on device"); return; } if (mode > QPainter::CompositionMode_Xor && !d->engine->hasFeature(QPaintEngine::BlendModes)) { qWarning("QPainter::setCompositionMode: Blend modes not supported on device"); return; } d->state->composition_mode = mode; d->state->dirtyFlags |= QPaintEngine::DirtyCompositionMode;}/*! Returns the current composition mode. \sa CompositionMode, setCompositionMode()*/QPainter::CompositionMode QPainter::compositionMode() const{ Q_D(const QPainter); return d->state->composition_mode;}/*! Returns the current background brush. \sa setBackground(), {QPainter#Settings}{Settings}*/const QBrush &QPainter::background() const{ Q_D(const QPainter); return d->state->bgBrush;}/*! Returns true if clipping has been set; otherwise returns false. \sa setClipping(), {QPainter#Clipping}{Clipping}*/bool QPainter::hasClipping() const{ Q_D(const QPainter); return d->state->clipEnabled && d->state->clipOperation != Qt::NoClip;}/*! Enables clipping if \a enable is true, or disables clipping if \a enable is false. \sa hasClipping(), {QPainter#Clipping}{Clipping}*/void QPainter::setClipping(bool enable){ Q_D(QPainter);#ifdef QT_DEBUG_DRAW if (qt_show_painter_debug_output) printf("QPainter::setClipping(), enable=%s, was=%s\n", enable ? "on" : "off", hasClipping() ? "on" : "off");#endif if (!isActive()) { qWarning("QPainter::setClipping: Painter not active, state will be reset by begin"); return; } if (hasClipping() == enable) return; // we can't enable clipping if we don't have a clip if (enable && (d->state->clipInfo.isEmpty() || d->state->clipInfo.last().operation == Qt::NoClip)) return; if (enable) d->state->clipEnabled = true; else d->state->clipEnabled = false; d->state->dirtyFlags |= QPaintEngine::DirtyClipEnabled; d->updateState(d->state);}/*! Returns the currently set clip region. Note that the clip region is given in logical coordinates. \sa setClipRegion(), clipPath(), setClipping()*/QRegion QPainter::clipRegion() const{ if (!isActive()) { qWarning("QPainter::clipRegion: Painter not active"); return QRegion(); } Q_D(const QPainter); QRegion region; bool lastWasNothing = true; if (!d->txinv) const_cast<QPainter *>(this)->d_ptr->updateInvMatrix(); for (int i=0; i<d->state->clipInfo.size(); ++i) { const QPainterClipInfo &info = d->state->clipInfo.at(i); QRegion other; switch (info.clipType) { case QPainterClipInfo::RegionClip: { QTransform matrix = (info.matrix * d->invMatrix); if (lastWasNothing) { region = info.region * matrix; lastWasNothing = false; continue; } if (info.operation == Qt::IntersectClip) region &= info.region * matrix; else if (info.operation == Qt::UniteClip) region |= info.region * matrix; else if (info.operation == Qt::NoClip) { lastWasNothing = true; region = QRegion(); } else region = info.region * matrix; break; } case QPainterClipInfo::PathClip: { QTransform matrix = (info.matrix * d->invMatrix); if (lastWasNothing) { region = QRegion((info.path * matrix).toFillPolygon().toPolygon(), info.path.fillRule()); lastWasNothing = false; continue; } if (info.operation == Qt::IntersectClip) { region &= QRegion((info.path * matrix).toFillPolygon().toPolygon(), info.path.fillRule()); } else if (info.operation == Qt::UniteClip) { region |= QRegion((info.path * matrix).toFillPolygon().toPolygon(), info.path.fillRule()); } else if (info.operation == Qt::NoClip) { lastWasNothing = true; region = QRegion(); } else { region = QRegion((info.path * matrix).toFillPolygon().toPolygon(), info.path.fillRule()); } break; } } } return region;}/*! Returns the currently clip as a path. Note that the clip path is given in logical coordinates. \sa setClipPath(), clipRegion(), setClipping()*/QPainterPath QPainter::clipPath() const{ // ### Since we do not support path intersections and path unions yet, // we just use clipRegion() here... if (!isActive()) { qWarning("QPainter::clipPath: Painter not active"); return QPainterPath(); } Q_D(const QPainter); // No clip, return empty if (d->state->clipInfo.size() == 0) { return QPainterPath(); } else { // Update inverse matrix, used below. if (!d->txinv) const_cast<QPainter *>(this)->d_ptr->updateInvMatrix(); // For the simple case avoid conversion. if (d->state->clipInfo.size() == 1 && d->state->clipInfo.at(0).clipType == QPainterClipInfo::PathClip) { QTransform matrix = (d->state->clipInfo.at(0).matrix * d->invMatrix); return d->state->clipInfo.at(0).path * matrix; // Fallback to clipRegion() for now, since we don't have isect/unite for paths } else { QPainterPath path; path.addRegion(clipRegion()); return path; } }}/*! \fn void QPainter::setClipRect(const QRectF &rectangle, Qt::ClipOperation operation) Enables clipping, and sets the clip region to the given \a rectangle using the given clip \a operation. The default operation is to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -