How PixmapItem in MainWindow:
main.cpp:
Qt Code:
  1. #include <QApplication>
  2. #include "diagramwindow.h"
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication app(argc, argv);
  6. DiagramWindow view;
  7. view.show();
  8. return app.exec();
  9. }
To copy to clipboard, switch view to plain text mode 
graphwidget.h:
Qt Code:
  1. #include <QApplication>
  2. #include "diagramwindow.h"
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication app(argc, argv);
  6. DiagramWindow view;
  7. view.show();
  8. return app.exec();
  9. }
To copy to clipboard, switch view to plain text mode 
graphwidget.cpp:
Qt Code:
  1. #include "graphwidget.h"
  2. #include "node.h"
  3. #include <QtGui>
  4. GraphWidget::GraphWidget(QWidget *parent)
  5. : QGraphicsView(parent)
  6. {
  7. QGraphicsScene *scene = new QGraphicsScene(this);
  8. scene->setSceneRect(-200, -200, 400, 400);
  9. setScene(scene);
  10. setRenderHint(QPainter::Antialiasing);
  11. setWindowTitle(tr("Elastic Nodes"));
  12. Node *node4 = new Node(this);
  13. centerNode = new Node(this);
  14. Node *node6 = new Node(this);
  15. scene->addItem(node4);
  16. scene->addItem(centerNode);
  17. scene->addItem(node6);
  18. node4->setPos(-50, 0);
  19. centerNode->setPos(0, 0);
  20. node6->setPos(50, 0);
  21. }
To copy to clipboard, switch view to plain text mode 
node.h:
Qt Code:
  1. #ifndef NODE_H
  2. #define NODE_H
  3. #include <QGraphicsPixmapItem>
  4. class GraphWidget;
  5. class Node : public QGraphicsPixmapItem
  6. {
  7. public:
  8. Node(GraphWidget *graphWidget);
  9. enum { Type = UserType + 1 };
  10. int type() const { return Type; }
  11. QRectF boundingRect() const;
  12. QPainterPath shape() const;
  13. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  14. private:
  15. GraphWidget *graph;
  16. };
To copy to clipboard, switch view to plain text mode 
node.cpp:
Qt Code:
  1. #include <QGraphicsScene>
  2. #include <QGraphicsSceneMouseEvent>
  3. #include <QPainter>
  4. #include <QStyleOption>
  5. #include "node.h"
  6. #include "graphwidget.h"
  7. Node::Node(GraphWidget *graphWidget)
  8. : graph(graphWidget)
  9. {
  10. setFlag(ItemIsMovable);
  11. setFlag(ItemSendsGeometryChanges);
  12. }
  13. QRectF Node::boundingRect() const
  14. {
  15. return QRectF(-24,-24,48,48);
  16. }
  17. QPainterPath Node::shape() const
  18. {
  19. path.addRect(-24, -24, 48, 48);
  20. return path;
  21. }
  22. void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
  23. {
  24. QPixmap pixmap = QPixmap("Hub.png");
  25. painter->drawPixmap(-24,-24,pixmap);
  26. }
To copy to clipboard, switch view to plain text mode 
What can be?