#include <QtGui>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#endif
class Arrow;
public:
setFlag(ItemSendsGeometryChanges, true);
}
protected:
if(event->modifiers() & Qt::ShiftModifier) {
setScale(scale()* (event->delta() > 0 ? 1.1 : 0.9));
} else {
setRotation(rotation()+(event->delta()/15.0));
}
}
if(change == ItemPositionHasChanged && scene()) {
foreach(Arrow *a, m_arrowStartsHere) updateStartArrowPosition(a);
foreach(Arrow *a, m_arrowEndsHere) updateEndArrowPosition(a);
}
}
void updateStartArrowPosition(Arrow *);
void updateEndArrowPosition(Arrow *);
private:
QList<Arrow*> m_arrowStartsHere;
QList<Arrow*> m_arrowEndsHere;
friend class Arrow;
};
public:
startItem = st;
endItem = en;
QPointF end
= mapFromItem
(endItem, endItem
->boundingRect
().
center());
setLine(0,0, end.x(), end.y());
startItem->m_arrowStartsHere << this;
endItem->m_arrowEndsHere << this;
f.setPointSize(8);
m_text->setFont(f);
updateArrowContent();
}
RotatingItem *start() const { return startItem; }
RotatingItem *end() const { return endItem; }
void updateArrowContent() {
m_text->setPos(line().pointAt(0.5));
m_text
->setText
(QString::number(line
().
length()));
}
protected:
RotatingItem *startItem, *endItem;
};
public:
protected:
p.drawText(viewport()->rect().adjusted(10,10,-10,-10), "Drag item to move\nWheel to rotate\nSHIFT+wheel to scale");
}
};
int main(int argc, char **argv) {
GraphicsView view;
view.setScene(&scene);
RotatingItem
*item1
= new RotatingItem
(QRect(-50,
-50,
100,
100));
RotatingItem
*item2
= new RotatingItem
(QRect(-50,
-50,
100,
100));
scene.addItem(item1);
scene.addItem(item2);
item1->setPos(100,100);
item2->setPos(200,200);
item1->setBrush(Qt::red);
item2->setBrush(Qt::yellow);
Arrow *arr = new Arrow(item1, item2);
p.setWidth(4);
p.setColor(Qt::blue);
arr->setPen(p);
item1->setZValue(10);
item2->setZValue(0);
arr->setZValue(10);
view.
setRenderHint(QPainter::Antialiasing);
view.resize(400,400);
view.show();
return app.exec();
}
void RotatingItem::updateStartArrowPosition(Arrow *arrow)
{
l.setP2(arrow->end()->mapToItem(arrow, arrow->end()->boundingRect().center()));
arrow->setLine(l);
arrow->updateArrowContent();
}
void RotatingItem::updateEndArrowPosition(Arrow *arrow)
{
l.setP2(mapToItem(arrow, boundingRect().center()));
arrow->setLine(l);
arrow->updateArrowContent();
}
#include <QtGui>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#endif
class Arrow;
class RotatingItem : public QGraphicsRectItem {
public:
RotatingItem(const QRectF &rect, QGraphicsItem *parent = 0) : QGraphicsRectItem(rect, parent){
setFlag(ItemSendsGeometryChanges, true);
}
protected:
void wheelEvent(QGraphicsSceneWheelEvent *event) {
if(event->modifiers() & Qt::ShiftModifier) {
setScale(scale()* (event->delta() > 0 ? 1.1 : 0.9));
} else {
setRotation(rotation()+(event->delta()/15.0));
}
}
QVariant itemChange(GraphicsItemChange change, const QVariant &value) {
if(change == ItemPositionHasChanged && scene()) {
foreach(Arrow *a, m_arrowStartsHere) updateStartArrowPosition(a);
foreach(Arrow *a, m_arrowEndsHere) updateEndArrowPosition(a);
}
return QGraphicsRectItem::itemChange(change, value);
}
void updateStartArrowPosition(Arrow *);
void updateEndArrowPosition(Arrow *);
private:
QList<Arrow*> m_arrowStartsHere;
QList<Arrow*> m_arrowEndsHere;
friend class Arrow;
};
class Arrow : public QGraphicsLineItem {
public:
Arrow(RotatingItem *st, RotatingItem *en) :QGraphicsLineItem(st) {
startItem = st;
endItem = en;
QPointF end = mapFromItem(endItem, endItem->boundingRect().center());
setLine(0,0, end.x(), end.y());
startItem->m_arrowStartsHere << this;
endItem->m_arrowEndsHere << this;
m_text = new QGraphicsSimpleTextItem(this);
QFont f;
f.setPointSize(8);
m_text->setFont(f);
updateArrowContent();
}
RotatingItem *start() const { return startItem; }
RotatingItem *end() const { return endItem; }
void updateArrowContent() {
m_text->setPos(line().pointAt(0.5));
m_text->setText(QString::number(line().length()));
}
protected:
RotatingItem *startItem, *endItem;
QGraphicsSimpleTextItem *m_text;
};
class GraphicsView : public QGraphicsView {
public:
GraphicsView(QWidget *parent = 0) : QGraphicsView(parent){}
protected:
void paintEvent(QPaintEvent *event) {
QGraphicsView::paintEvent(event);
QPainter p(viewport());
p.drawText(viewport()->rect().adjusted(10,10,-10,-10), "Drag item to move\nWheel to rotate\nSHIFT+wheel to scale");
}
};
int main(int argc, char **argv) {
QApplication app(argc, argv);
GraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);
RotatingItem *item1 = new RotatingItem(QRect(-50, -50, 100, 100));
RotatingItem *item2 = new RotatingItem(QRect(-50, -50, 100, 100));
scene.addItem(item1);
scene.addItem(item2);
item1->setPos(100,100);
item2->setPos(200,200);
item1->setFlag(QGraphicsItem::ItemIsMovable);
item2->setFlag(QGraphicsItem::ItemIsMovable);
item1->setBrush(Qt::red);
item2->setBrush(Qt::yellow);
Arrow *arr = new Arrow(item1, item2);
QPen p;
p.setWidth(4);
p.setColor(Qt::blue);
arr->setPen(p);
item1->setZValue(10);
item2->setZValue(0);
arr->setZValue(10);
view.setRenderHint(QPainter::Antialiasing);
view.resize(400,400);
view.show();
return app.exec();
}
void RotatingItem::updateStartArrowPosition(Arrow *arrow)
{
QLineF l = arrow->line();
l.setP2(arrow->end()->mapToItem(arrow, arrow->end()->boundingRect().center()));
arrow->setLine(l);
arrow->updateArrowContent();
}
void RotatingItem::updateEndArrowPosition(Arrow *arrow)
{
QLineF l = arrow->line();
l.setP2(mapToItem(arrow, boundingRect().center()));
arrow->setLine(l);
arrow->updateArrowContent();
}
To copy to clipboard, switch view to plain text mode
Bookmarks