Qt4 in Qt5: QPainter :: drawPixmapFragments () with 5 arguments - how to solve? (Updated)

Qt 4.8 (4.8.6) has an overloaded QPainter :: drawPixmapFragments () function with 5 arguments:

void drawPixmapFragments(const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount,
                         const QPixmap &pixmap, PixmapFragmentHints hints = 0);

Qt 5 (5.4.1) does not have such a function, it has only one (the same as in Qt 4.8) with 4 arguments:

void drawPixmapFragments(const PixmapFragment *fragments, int fragmentCount,
                         const QPixmap &pixmap, PixmapFragmentHints hints = 0);

I searched in wiki.qt.io, here in stackoverflow and several other places, but there is no answer how to port it from Qt 4.8 to Qt 5.

Does anyone know how to do this?

UPD I took the implementation from Qt 4.8.6 source ( qpainter.cpp) and simply convert it to move the pointer to QPainter as the first parameter:

namespace oldqt
{
    void drawPixmapFragments(QPainter *painter, const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount,
        const QPixmap &pixmap, QPainter::PixmapFragmentHints hints)
    {
        // Q_D(QPainter); 

        if (/* !d->engine || */ pixmap.isNull())
            return;

    #ifndef QT_NO_DEBUG
        if (sourceRects) {
            for (int i = 0; i < fragmentCount; ++i) {
                QRectF sourceRect = sourceRects[i];
                if (!(QRectF(pixmap.rect()).contains(sourceRect)))
                    qWarning("QPainter::drawPixmapFragments - the source rect is not contained by the pixmap rectangle");
            }
        }
    #endif

    // if (d->engine->isExtended()) {
    //              d->extended->drawPixmapFragments(targetRects, sourceRects, fragmentCount, pixmap, hints);
    //          }
    //          else {
            if (sourceRects) {
                for (int i = 0; i < fragmentCount; ++i)
                    painter->drawPixmap(targetRects[i], pixmap, sourceRects[i]);
            }
            else {
                QRectF sourceRect = pixmap.rect();
                for (int i = 0; i < fragmentCount; ++i)
                    painter->drawPixmap(targetRects[i], pixmap, sourceRect);
            }
    //          }

    }
}

But I commented on some lines. Q_D(QPainter)somehow determines dfrom d_func, how can I take it from *painter? Or is it impossible? Maybe there is another solution?

UPD2 :

class ButtonSelector:public QGraphicsObject
// ...

void ButtonSelector::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
    //painter->drawPixmap(m_backGnd.rect(), m_backGnd);
    QRectF rectSrc = QRectF(m_backGnd.rect());
    QRectF rectTrg = boundingRect();
    painter->drawPixmapFragments(&rectTrg,&rectSrc,1,m_backGnd, QPainter::OpaqueHint);
    // I've change it to this call:
    // oldqt::drawPixmapFragments(painter, &rectTrg, &rectSrc, 1, m_backGnd, QPainter::OpaqueHint);
    // where oldqt::drawPixmapFragments is function from above

    // ... some other code unrelated to the question
}

, . ?

@amartel:

void drawPixmapFragments(QPainter *painter, const QRectF *targetRects, const QRectF *sourceRects, int fragmentCount,
    const QPixmap &pixmap, QPainter::PixmapFragmentHints hints)
{
    for (int i = 0; i < fragmentCount; ++i) {
        QRectF sourceRect = (sourceRects) ? sourceRects[i] : pixmap.rect();

        QPainter::PixmapFragment pixmapFragment =
            QPainter::PixmapFragment::create(
                targetRects[i].center(),
                sourceRects[i],
                targetRects[i].width() / sourceRect.width(),
                targetRects[i].height() / sourceRect.height()
            );

        painter->drawPixmapFragments(&pixmapFragment, 1, pixmap, hints);
    }
}
+4
2

http://qt.apidoc.info/5.1.1/qtgui/qpainter-pixmapfragment.html Qt5, :

void ButtonSelector::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
    //painter->drawPixmap(m_backGnd.rect(), m_backGnd);
    QRectF rectSrc = QRectF(m_backGnd.rect());
    QRectF rectTrg = boundingRect();
    QPainter::PixmapFragment fragment = QPainter::PixmapFragment::create(rectTrg.center(), rectSrc, rectTrg.width() / rectSrc.width(), rectTrg.height() / rectSrc.height());
    painter->drawPixmapFragments(&fragment,1,m_backGnd, QPainter::OpaqueHint);
    // I've change it to this call:
    // oldqt::drawPixmapFragments(painter, &rectTrg, &rectSrc, 1, m_backGnd, QPainter::OpaqueHint);
    // where oldqt::drawPixmapFragments is function from above

    // ... some other code unrelated to the question
}
+2

, "" :

: http://qt.apidoc.info/5.1.1/qtgui/qpainter-pixmapfragment.html

sourceLeft, sourceTop, width height , QPainter:: drawPixmapFragments(). x, y, width height . x y . scaleX scaleY. x, y.

0

All Articles