c – Qt4到Qt5:带有5个参数的QPainter :: drawPixmapFragments() – 如何解决?

Qt 4.8(4.8.6)有一个带有5个参数的QPainter :: drawPixmapFragments()重载函数:

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

Qt 5(5.4.1)没有这样的功能,只有一个(与Qt 4.8相同)有4个参数:

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

我在wiki.qt.io上搜索了这里,在stackoverflow和其他几个地方,但没有答案如何将它从Qt 4.8移植到Qt 5.

怎么做?

UPD我从Qt 4.8.6源代码(qpainter.cpp)中实现并简单地将其转换为将指向QPainter的指针作为第一个参数:

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's 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);
            }
    //          }

    }
}

但我评论了一些问题. Q_D(QPainter)以某种方式从d_func定义d,我如何从* painter中获取它?还是不可能?
可能还有另一种解决方案吗?

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
}

原则上,它工作正常.代码中有什么不对?

解决方案基于answer@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);
    }
}

最佳答案 根据Qt5中的
http://qt.apidoc.info/5.1.1/qtgui/qpainter-pixmapfragment.html,您的代码应如下所示:

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
}
点赞