#include <QtGui>
public:
Canvas(){
m_item = 0;
}
m_item = item;
m_item->setPos(itemPos);
}
void setItemPos
(const QPointF &pt
) { itemPos = pt.toPoint();
if(m_item)
m_item->setPos(itemPos);
}
protected:
if(m_item){
painter->save();
painter
->setPen
(QPen(Qt
::blue));
painter->setBrush(Qt::blue);
painter->drawEllipse(m_item->pos(), 5, 5);
painter->restore();
}
}
if(m_item && me->modifiers() & Qt::ShiftModifier){
m_item->setPos(me->pos());
}
}
if(event->delta()>0) {
setScale(scale()*1.1);
} else if(event->delta()<0) {
setScale(scale()/1.1);
}
}
private:
};
public:
protected:
painter->save();
painter
->setPen
(QPen(Qt
::black));
painter->setBrush(Qt::black);
painter
->drawEllipse
(QPointF(0,
0),
3,
3);
painter->restore();
}
if(me->modifiers() & Qt::ControlModifier){
r.translate(-me->pos());
setRect(r);
} else me->ignore();
}
};
int main(int argc, char **argv) {
view.setScene(&scene);
Canvas *canvas = new Canvas;
canvas->setRect(0,0,400,400);
scene.addItem(canvas);
Item *item = new Item(canvas);
canvas->setItem(item);
item
->setRect
(QRect(-100,
-50,
200,
100));
item
->setBrush
(QColor(255,
0,
0,
150));
item
->setPen
(QPen(Qt
::black));
canvas->setItemPos(canvas->boundingRect().center());
view.show();
return app.exec();
}
#include <QtGui>
class Canvas : public QGraphicsRectItem {
public:
Canvas(){
m_item = 0;
}
void setItem(QGraphicsItem *item) {
m_item = item;
m_item->setPos(itemPos);
}
void setItemPos(const QPointF &pt) {
itemPos = pt.toPoint();
if(m_item)
m_item->setPos(itemPos);
}
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0){
QGraphicsRectItem::paint(painter, option, widget);
if(m_item){
painter->save();
painter->setPen(QPen(Qt::blue));
painter->setBrush(Qt::blue);
painter->drawEllipse(m_item->pos(), 5, 5);
painter->restore();
}
}
void mousePressEvent(QGraphicsSceneMouseEvent *me) {
if(m_item && me->modifiers() & Qt::ShiftModifier){
m_item->setPos(me->pos());
}
}
void wheelEvent(QGraphicsSceneWheelEvent * event){
if(event->delta()>0) {
setScale(scale()*1.1);
} else if(event->delta()<0) {
setScale(scale()/1.1);
}
}
private:
QPoint itemPos;
QGraphicsItem *m_item;
};
class Item : public QGraphicsRectItem {
public:
Item(QGraphicsItem *parent = 0) : QGraphicsRectItem(parent){}
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0){
QGraphicsRectItem::paint(painter, option, widget);
painter->save();
painter->setPen(QPen(Qt::black));
painter->setBrush(Qt::black);
painter->drawEllipse(QPointF(0,0), 3, 3);
painter->restore();
}
void mousePressEvent(QGraphicsSceneMouseEvent *me) {
if(me->modifiers() & Qt::ControlModifier){
QRectF r = rect();
r.translate(-me->pos());
setRect(r);
} else me->ignore();
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
QGraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);
Canvas *canvas = new Canvas;
canvas->setRect(0,0,400,400);
scene.addItem(canvas);
Item *item = new Item(canvas);
canvas->setItem(item);
item->setRect(QRect(-100,-50,200,100));
item->setBrush(QColor(255,0,0,150));
item->setPen(QPen(Qt::black));
item->setFlag(QGraphicsItem::ItemIgnoresTransformations);
canvas->setItemPos(canvas->boundingRect().center());
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks