Qt - 有没有办法将图形项转换为像素图?

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

我想从图形对象创建一个像素图,所以我可以将像素图设置为图标

我有一个 Block class 派生自 QGraphicsPathItem 我尝试使用:

Block *block = new Block();

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

QPixmap pixmapItem;
pixmapItem.copy(rect);

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

但像素图似乎是空的。

有没有办法从 graphicObject 或 graphicItem 中获取 image/icon?

应该可以使用 QGraphicsItem::paint 为此:

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

QPainter pixmapPainter(&targetPixmap);
QStyleOptionGraphicsItem styleOption;
item->paint(&pixmapPainter, &styleOption);

你必须使用QGraphicsItempaint()方法来获取渲染:

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;
}

示例:

#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();
}

这是因为边界矩形仅包含有关您的 QGraphicsItem 的坐标和大小信息,但没有有关如何绘制它的更多信息。

您可以尝试类似于以下的操作:创建一个 block 大小的 QImage 并使用它来初始化 QPainter。然后 block 可以使用 QPainter 在图像上绘制。这是使用 paint 方法实现的,该方法 Block 继承自 QGraphicsItem

Block *block = new Block();
QSize size = block->boundingRect().toRect().toSize();
QImage* image = new QImage(size, QImage::Format_RGB32);
QPainter* painter = new QPainter(image);
block->paint(painter, new StyleOptionGraphicsItem());

那你的block应该画成image.

感谢 @eyllanesc 提供的很好的示例。此外,翻译命令在某些情况下也是强制性的。

 static QPixmap QPixmapFromItem(QGraphicsItem *item){
            QPixmap pixmap(item->boundingRect().size().toSize());
            pixmap.fill(Qt::transparent);
            QPainter painter(&pixmap);
            painter.setRenderHint(QPainter::Antialiasing);
            QStyleOptionGraphicsItem opt;
            painter.translate (-1 * (item->boundingRect ().topLeft ()));//THIS IS MANDATORY IN SOME CASES.
            item->paint(&painter, &opt);
            return pixmap;
        }