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

时间:2018-05-15 20:16:08

标签: c++ qt qt5 qgraphicsitem

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

我有一个来自Block的{​​{1}}类,我尝试使用:

QGraphicsPathItem

但像素图似乎是空的。

有没有办法从graphicObject或graphicItem中获取图像/图标?

4 个答案:

答案 0 :(得分:3)

您必须使用QGraphicsItem的{​​{3}}方法来获取渲染:

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

paint()

答案 1 :(得分:2)

应该可以使用QGraphicsItem::paint

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

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

答案 2 :(得分:1)

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

您可以尝试类似以下内容:创建QImage的{​​{1}}大小,并使用它来初始化block。然后,QPainter可以使用QPainter来绘制图像。这是使用block继承自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

答案 3 :(得分:0)

感谢@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;
        }