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)
{
if ( 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 (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);
QRectF rectSrc = QRectF(m_backGnd.rect());
QRectF rectTrg = boundingRect();
painter->drawPixmapFragments(&rectTrg,&rectSrc,1,m_backGnd, QPainter::OpaqueHint);
}
, . ?
@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);
}
}