#ifndef MYITEM_H
#define MYITEM_H
#include <QGraphicsItem>
class MyItem : public QGraphicsItem
{
public:
MyItem();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
#endif // MYITEM_H
#include "myitem.h"
#include <QPainter>
#include <QRectF>
MyItem::MyItem()
{
}
QRectF MyItem::boundingRect() const
{
qreal adjust=0.5;
return QRectF(-18-adjust,-22-adjust,
36+adjust,60+adjust);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(0,0,20,20);
}
#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include "myitem.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
scene.setSceneRect(-300,-300,600,600);
scene.setItemIndexMethod(QGraphicsScene::NoIndex);
MyItem *item=new MyItem;
scene.addItem(item);
QGraphicsView view(&scene);
view.setRenderHint(QPainter::Antialiasing);
view.setCacheMode(QGraphicsView::CacheBackground);
view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
view.setDragMode(QGraphicsView::ScrollHandDrag);
view.resize(400,300);
view.show();
return a.exec();
}