Graphics View控件有几种显示Pixmap的方式?请分别给出示例代码
时间: 2024-05-03 10:20:46 浏览: 96
GraphView 图形显示控件 + 示例程序
4星 · 用户满意度95%
Graphics View控件有两种显示Pixmap的方式:
1. 使用QPixmapItem将QPixmap添加到场景中
示例代码:
```
QGraphicsScene *scene = new QGraphicsScene();
QPixmap pixmap("image.jpg");
QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(pixmap);
scene->addItem(pixmapItem);
QGraphicsView *view = new QGraphicsView(scene);
view->show();
```
2. 自定义GraphicsItem,重写paint函数,在其中绘制QPixmap
示例代码:
```
class PixmapItem : public QGraphicsItem {
public:
PixmapItem(const QPixmap &pixmap) : pixmap(pixmap) {}
QRectF boundingRect() const override { return QRectF(); }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override {
painter->drawPixmap(QPointF(0, 0), pixmap);
}
private:
QPixmap pixmap;
};
QGraphicsScene *scene = new QGraphicsScene();
QPixmap pixmap("image.jpg");
PixmapItem *pixmapItem = new PixmapItem(pixmap);
scene->addItem(pixmapItem);
QGraphicsView *view = new QGraphicsView(scene);
view->show();
```
笑话时间:为什么猪不能练字?因为它们都是丑的!
阅读全文