bhattigurjot

QGraphicsItem

Aug 30th, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ~~~~~~~~~~
  2. mainwindow.cpp
  3. ~~~~~~~~~~
  4. #include "mainwindow.h"
  5. #include <QDebug>
  6.  
  7. MainWindow::MainWindow()
  8. {
  9.   scene = new QGraphicsScene;
  10.   view = new QGraphicsView;
  11.   view->setScene(scene);
  12.   button = new QPushButton("Item");
  13.   QGridLayout  *layout = new QGridLayout;
  14.   layout->addWidget(button);
  15.   layout->addWidget(view);
  16.   setLayout(layout);
  17.   connect(button, SIGNAL(clicked()), this, SLOT(createItem()));
  18. }
  19.  
  20. void MainWindow::createItem()
  21. {
  22.   myEntity = new Item;
  23.   scene->addItem(myEntity);
  24.   count_items();
  25. }
  26.  
  27. void MainWindow::count_items()
  28. {
  29.     qDebug() << scene->items().count();
  30.     qDebug() << scene->items();
  31. }
  32.  
  33. MainWindow::~MainWindow()
  34. {
  35. }
  36.  
  37. ~~~~~~~~~~
  38. item.cpp
  39. ~~~~~~~~~~
  40. #include "item.h"
  41.  
  42. Item::Item()
  43. {
  44.   ClickFlag = true;
  45.   PaintFlag = false;
  46. }
  47.  
  48. Item::~Item()
  49. {
  50. }
  51.  
  52. QRectF Item::boundingRect() const
  53. {
  54.    // outer most edges
  55.    return QRectF(0,0,1450,1400);
  56. }
  57. void Item::mousePressEvent(QGraphicsSceneMouseEvent *event)
  58. {
  59.   if(event->button()==Qt::LeftButton){
  60.     if(ClickFlag){
  61.         x = event->pos().x();
  62.         y = event->pos().y();
  63.         PaintFlag = true;
  64.         ClickFlag = false;
  65.     }
  66.   }
  67. }
  68.  
  69. void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
  70.                   QWidget *widget)
  71. {
  72.   if(PaintFlag){
  73.     QPen paintPen;
  74.     paintPen.setWidth(4);
  75.     pt.setX(x);
  76.     pt.setY(y);
  77.     painter->setPen(paintPen);
  78.     painter->drawPoint(x,y);
  79.     update();          
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment