NodeAndLink.rar
Qt 场景、视图、项应用实例,Qt整套源码,喜欢请下载: Link: #include <QGraphicsLineItem> class Node; class Link : public QGraphicsLineItem { public: Link(Node *formN, Node *toN); ~Link(); inline Node *formNode()const { return myFormNode; } inline Node *toNode() const { return myToNode; } void setColor(const QColor &c); QColor color()const; void trackNodes(); private: Node *myFormNode; Node *myToNode; }; Node: #ifndef NODE_H #define NODE_H #include <QGraphicsItem> #include <QSet> class Link; class Node : public QGraphicsItem { public: Node(); ~Node(); inline void setText(const QString &text;); inline QString text()const {return myText;} inline void setTextColor(const QColor &c) { myTextColor = c; update();} inline QColor textColor()const{return myTextColor;} inline void setBackgroundColor(const QColor &c) { myBackgroundColor = c; update();} inline QColor backgroundColor()const{return myBackgroundColor;} inline void setOutLineColor(const QColor &c) { myOutLineColor = c; update();} inline QColor outLineColor()const{return myOutLineColor;} void addLink(Link *l); void removeLink(Link *l); QRectF boundingRect()const Q_DECL_OVERRIDE; QPainterPath shape() const Q_DECL_OVERRIDE; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = Q_NULLPTR) Q_DECL_OVERRIDE; protected: void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; QVariant itemChange(GraphicsItemChange change, const QVariant &value;) Q_DECL_OVERRIDE; private: QRectF outlineRect()const; int roundness(double size)const;//计算圆角 QSet<Link *> myLinks; QString myText; QColor myTextColor; QColor myBackgroundColor; QColor myOutLineColor; }; #endif // NODE_H View: #ifndef GRAPHICSVIEW_H #define GRAPHICSVIEW_H #include <QWidget> #include <QGraphicsView> #include <QGraphicsScene> class GraphicsView : public QGraphicsView { Q_OBJECT public: explicit GraphicsView(QGraphicsScene *scene, QWidget *parent = 0); protected: void wheelEvent(QWheelEvent *e); }; #endif // GRAPHICSVIEW_H