//map.h#include <QGraphicsItem>#include <QPainter>class Map : public QGraphicsRectItem{public: Map(QImage img); ~Map(); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);private: QImage image; };
#include <QStyleOptionGraphicsItem>#include "map.h"Map::Map(QImage img) : image(img){}Map::~Map(){}void Map::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){ painter->drawImage( option->exposedRect, image, option->exposedRect, Qt::OrderedAlphaDither );}
#include "map.h"... canvas = new QGraphicsScene(); editor = new QGraphicsView(canvas, ui.centralwidget); editor->setDragMode(QGraphicsView::RubberBandDrag); ui.vboxLayout->addWidget(editor);... // Вот это работает QAbstractGraphicsShapeItem* i = canvas->addEllipse(QRectF(0,0,50,50)); i->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); i->setPen(Qt::NoPen); i->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); i->setPos(qrand()%int(canvas->width()), qrand()%int(canvas->height())); i->setZValue(qrand()%256);... QString logo_fn = "qtlogo.png"; QImage logoimg; logoimg.load(logo_fn); // А вот это нет Map *map = new Map(logoimg); map->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); map->setPen(Qt::NoPen); map->setBrush( QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8) ); map->setPos(qrand()%int(canvas->width()), qrand()%int(canvas->height())); map->setZValue(qrand()%256); canvas->addItem(map);