Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - is there a way to transform a graphic item into a pixmap?

I want to create a pixmap from a graphicObject, so i can set the pixmap as an icon

I have a Block class derived from QGraphicsPathItem and i tried using:

Block *block = new Block();

QRect rect = block->boundingRect().toRect();

QPixmap pixmapItem;
pixmapItem.copy(rect);

QListWidgetItem *item = new QListWidgetItem;
item->setIcon(QPixmap(pixmapItem));

but the pixmap appears to be empty.

Is there a way to get an image/icon out of a graphicObject or graphicItem?

like image 795
Gabriel_Br Avatar asked May 15 '18 20:05

Gabriel_Br


People also ask

What is the use of pixmap in qgraphicspixmapitem?

The pixmap () function returns the current pixmap. QGraphicsPixmapItem uses pixmap's optional alpha mask to provide a reasonable implementation of boundingRect (), shape (), and contains (). The pixmap is drawn at the item's (0, 0) coordinate, as returned by offset ().

How to detach a pixmap in Qt?

See also defaultDepth () and Pixmap Information. Detaches the pixmap from shared pixmap data. A pixmap is automatically detached by Qt whenever its contents are about to change. This is done in almost all QPixmap member functions that modify the pixmap ( fill (), fromImage (), load (), etc.), and in QPainter::begin () on a pixmap.

How do I use transformations in qpainter?

The Transformations example shows how transformations influence the way that QPainter renders graphics primitives. The application allows the user to manipulate the rendering of a shape by changing the translation, rotation and scale of QPainter 's coordinate system. The RenderArea class controls the rendering of a given shape.

How do I convert a qimage to a qpixmap?

Likewise, a QImage can be converted into a QPixmap using the fromImage (). If this is too expensive an operation, you can use QBitmap::fromImage () instead. To convert a QPixmap to and from HICON you can use the QtWinExtras functions QtWin::toHICON () and QtWin::fromHICON () respectively.


2 Answers

You have to use the paint() method of QGraphicsItem to get the rendering:

static QPixmap QPixmapFromItem(QGraphicsItem *item){
    QPixmap pixmap(item->boundingRect().size().toSize());
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    QStyleOptionGraphicsItem opt;
    item->paint(&painter, &opt);
    return pixmap;
}

Example:

#include <QApplication>
#include <QGraphicsPathItem>
#include <QGraphicsView>
#include <QHBoxLayout>
#include <QListWidget>

static QPixmap QPixmapFromItem(QGraphicsItem *item){
    QPixmap pixmap(item->boundingRect().size().toSize());
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    QStyleOptionGraphicsItem opt;
    item->paint(&painter, &opt);
    return pixmap;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QHBoxLayout lay(&w);
    QListWidget listWidget;
    QGraphicsView view;
    QGraphicsScene scene;
    view.setScene(&scene);

    int counter = 0;
    for(const QColor & color : {QColor("red"), QColor("blue"), QColor("green")}){
        QPainterPath p;
        p.addRoundedRect(0, 0, 150, 50, 2, 2);
        QGraphicsPathItem *block = scene.addPath(p);
        block->setBrush(QBrush(color));
        block->setFlag(QGraphicsItem::ItemIsMovable);
        block->setFlag(QGraphicsItem::ItemIsSelectable);
        block->setPos(counter*QPointF(10, 10));

        // get QPixmap from item
        QPixmap pixmap = QPixmapFromItem(block); 

        QListWidgetItem *l_item = new QListWidgetItem(color.name());
        listWidget.addItem(l_item);
        l_item->setIcon(QIcon(pixmap));
        counter++;
    }
    lay.addWidget(&listWidget);
    lay.addWidget(&view);
    w.show();
    return a.exec();
}

enter image description here

like image 89
eyllanesc Avatar answered Sep 28 '22 09:09

eyllanesc


It should be possible to use QGraphicsItem::paint for this:

QSize itemSize = item->boundingRect()
QPixmap targetPixmap(item->boundingRect().size().toSize());

QPainter pixmapPainter(&targetPixmap);
QStyleOptionGraphicsItem styleOption;
item->paint(&pixmapPainter, &styleOption);
like image 37
joe_chip Avatar answered Sep 28 '22 08:09

joe_chip