Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

bool QPainter::begin ( QPaintDevice device ) [inherited]

Begins painting the paint device and returns true if successful; otherwise returns false.

Notice that all painter settings (setPen(), setBrush() etc.) are reset to default values when begin() is called.

The errors that can occur are serious problems, such as these:

doc/src/snippets/code/src_gui_painting_qpainter.cpp 3

Note that most of the time, you can use one of the constructors instead of begin(), and that end() is automatically done at destruction.

Warning:
A paint device can only be painted by one painter at a time.
See also:
end(), QPainter()

Definition at line 1629 of file qpainter.cpp.

References QPixmap::depth(), QImage::depth(), QPainter::device(), QPainter::end(), QRect::height(), QPainter::initFrom(), QWidget::internalWinId(), QRect::isEmpty(), QImage::isNull(), QPixmap::isNull(), QPainter::layoutDirection(), QWidget::mapTo(), QWidget::nativeParentWidget(), QPainter::redirected(), QWidget::testAttribute(), and QRect::width().

Referenced by Window::addBlock(), begin(), SourceWidget::createData(), QWindowsStyle::drawComplexControl(), PaintWidget::paintEvent(), ArthurFrame::paintEvent(), Window::paintEvent(), EmbedWidget::paintEvent(), QFDProgressAnimation::paintEvent(), PuzzleWidget::paintEvent(), Widget::paintEvent(), QtGradientStopsWidget::paintEvent(), DisplayWidget::paintEvent(), GLWidget::paintEvent(), Q3IconViewItem::paintItem(), NorwegianWoodStyle::polish(), Object::print(), QWebFrame::print(), QPainter::QPainter(), Window::saveSvg(), and TetrixBoard::update().

{
    Q_ASSERT(pd);

    if (pd->painters > 0) {
        qWarning("QPainter::begin: A paint device can only be painted by one painter at a time.");
        return false;
    }

    if (d_ptr->engine) {
        qWarning("QPainter::begin: Painter already active");
        return false;
    }

    if (QPainterPrivate::attachPainterPrivate(this, pd))
        return true;

    Q_D(QPainter);

    d->helper_device = pd;
    d->original_device = pd;
    QPaintDevice *rpd = 0;

    QPoint redirectionOffset;
    // We know for sure that redirection is broken when the widget is inside
    // its paint event, so it's safe to use our hard-coded redirection. However,
    // there IS one particular case we still need to support, and that's
    // when people call QPainter::setRedirected in the widget's paint event right
    // before any painter is created (or QPainter::begin is called). In that
    // particular case our hard-coded redirection is restored and the redirection
    // is retrieved from QPainter::redirected (as before).
    if (pd->devType() == QInternal::Widget)
        rpd = static_cast<QWidget *>(pd)->d_func()->redirected(&redirectionOffset);

    if (!rpd)
        rpd = redirected(pd, &redirectionOffset);

    if (rpd)
        pd = rpd;

#ifdef QT_DEBUG_DRAW
    if (qt_show_painter_debug_output)
        printf("QPainter::begin(), device=%p, type=%d\n", pd, pd->devType());
#endif


    d->device = pd;
    if (pd->devType() == QInternal::Pixmap)
        static_cast<QPixmap *>(pd)->detach();
    else if (pd->devType() == QInternal::Image)
        static_cast<QImage *>(pd)->detach();

    d->engine = pd->paintEngine();
    d->extended = d->engine && d->engine->isExtended() ? static_cast<QPaintEngineEx *>(d->engine) : 0;
    if (d->emulationEngine)
        d->emulationEngine->real_engine = d->extended;

    // Setup new state...
    Q_ASSERT(!d->state);
    d->state = d->extended ? d->extended->createState(0) : new QPainterState;
    d->state->painter = this;
    d->states.push_back(d->state);

    d->state->redirection_offset = redirectionOffset;
    d->state->brushOrigin = QPointF();

    if (!d->engine) {
        qWarning("QPainter::begin: Paint device returned engine == 0, type: %d", pd->devType());
        return true;
    }

    // Slip a painter state into the engine before we do any other operations
    if (d->extended)
        d->extended->setState(d->state);
    else
        d->engine->state = d->state;

    switch (pd->devType()) {
        case QInternal::Widget:
        {
            const QWidget *widget = static_cast<const QWidget *>(pd);
            Q_ASSERT(widget);

            const bool paintOutsidePaintEvent = widget->testAttribute(Qt::WA_PaintOutsidePaintEvent);
            const bool inPaintEvent = widget->testAttribute(Qt::WA_WState_InPaintEvent);
            if(!d->engine->hasFeature(QPaintEngine::PaintOutsidePaintEvent)
                && !paintOutsidePaintEvent && !inPaintEvent) {
                qWarning("QPainter::begin: Widget painting can only begin as a "
                         "result of a paintEvent");
                d->engine = 0;
                d->device = 0;
                return false;
            }

            // Adjust offset for alien widgets painting outside the paint event.
            if (!inPaintEvent && paintOutsidePaintEvent && !widget->internalWinId()
                && widget->testAttribute(Qt::WA_WState_Created)) {
                d->state->redirection_offset -= widget->mapTo(widget->nativeParentWidget(), QPoint());
            }
            break;
        }
        case QInternal::Pixmap:
        {
            QPixmap *pm = static_cast<QPixmap *>(pd);
            Q_ASSERT(pm);
            if (pm->isNull()) {
                qWarning("QPainter::begin: Cannot paint on a null pixmap");
                d->engine = 0;
                d->device = 0;
                return false;
            }

            if (pm->depth() == 1) {
                d->state->pen = QPen(Qt::color1);
                d->state->brush = QBrush(Qt::color0);
            }
            break;
        }
        case QInternal::Image:
        {
            QImage *img = static_cast<QImage *>(pd);
            Q_ASSERT(img);
            if (img->isNull()) {
                qWarning("QPainter::begin: Cannot paint on a null image");
                d->engine = 0;
                d->device = 0;
                return false;
            }
            if (img->depth() == 1) {
                d->state->pen = QPen(Qt::color1);
                d->state->brush = QBrush(Qt::color0);
            }
            break;
        }
        default:
            break;
    }
    if (d->state->ww == 0) // For compat with 3.x painter defaults
        d->state->ww = d->state->wh = d->state->vw = d->state->vh = 1024;

    d->engine->setPaintDevice(pd);

    bool begun = d->engine->begin(pd);
    if (!begun) {
        qWarning("QPainter::begin(): Returned false");
        if (d->engine->isActive()) {
            end();
        } else {
            d->states.clear();
            delete d->state;
            d->state = 0;
        }
        d->engine = 0;
        d->device = 0;
        return false;
    } else {
        d->engine->setActive(begun);
    }

    // Copy painter properties from original paint device,
    // required for QPixmap::grabWidget()
    if (d->original_device->devType() == QInternal::Widget) {
        QWidget *widget = static_cast<QWidget *>(d->original_device);
        initFrom(widget);
    } else {
        d->state->layoutDirection = QApplication::layoutDirection();
        // make sure we have a font compatible with the paintdevice
        d->state->deviceFont = d->state->font = QFont(d->state->deviceFont, device());
    }

    QRect systemRect = d->engine->systemRect();
    if (!systemRect.isEmpty()) {
        d->state->ww = d->state->vw = systemRect.width();
        d->state->wh = d->state->vh = systemRect.height();
    } else {
        d->state->ww = d->state->vw = pd->metric(QPaintDevice::PdmWidth);
        d->state->wh = d->state->vh = pd->metric(QPaintDevice::PdmHeight);
    }

    d->state->redirection_offset += d->engine->coordinateOffset();

    Q_ASSERT(d->engine->isActive());

    if (!d->state->redirection_offset.isNull())
        d->updateMatrix();

    Q_ASSERT(d->engine->isActive());
    d->state->renderHints = QPainter::TextAntialiasing;
    ++d->device->painters;

    d->state->emulationSpecifier = 0;

    return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index