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?
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 ().
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.
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.
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.
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();
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With