Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ~~~~~~~~~~
- mainwindow.cpp
- ~~~~~~~~~~
- #include "mainwindow.h"
- #include <QDebug>
- MainWindow::MainWindow()
- {
- scene = new QGraphicsScene;
- view = new QGraphicsView;
- view->setScene(scene);
- button = new QPushButton("Item");
- QGridLayout *layout = new QGridLayout;
- layout->addWidget(button);
- layout->addWidget(view);
- setLayout(layout);
- connect(button, SIGNAL(clicked()), this, SLOT(createItem()));
- }
- void MainWindow::createItem()
- {
- myEntity = new Item;
- scene->addItem(myEntity);
- count_items();
- }
- void MainWindow::count_items()
- {
- qDebug() << scene->items().count();
- qDebug() << scene->items();
- }
- MainWindow::~MainWindow()
- {
- }
- ~~~~~~~~~~
- item.cpp
- ~~~~~~~~~~
- #include "item.h"
- Item::Item()
- {
- ClickFlag = true;
- PaintFlag = false;
- }
- Item::~Item()
- {
- }
- QRectF Item::boundingRect() const
- {
- // outer most edges
- return QRectF(0,0,1450,1400);
- }
- void Item::mousePressEvent(QGraphicsSceneMouseEvent *event)
- {
- if(event->button()==Qt::LeftButton){
- if(ClickFlag){
- x = event->pos().x();
- y = event->pos().y();
- PaintFlag = true;
- ClickFlag = false;
- }
- }
- }
- void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
- QWidget *widget)
- {
- if(PaintFlag){
- QPen paintPen;
- paintPen.setWidth(4);
- pt.setX(x);
- pt.setY(y);
- painter->setPen(paintPen);
- painter->drawPoint(x,y);
- update();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment