PDA

View Full Version : Can't get positions of graphics items in scene



Kamalpreet
30th August 2014, 08:14
Hi,

I am trying to get the positions of the graphicsitems in the scene.
But their QPointF value always remains (0,0).

I am painting when mouse-click event occurs. On debugging scene->items(), I get

(QGraphicsItem(this =0x22edff0, parent =0x0, pos =QPointF(0, 0) , z = 0 , flags = ( ) ) )

for each graphics item in scene but with different memory address.

This is my mainwindow.cpp code:


#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()
{
}


This is my item.cpp code:


#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();
}
}


I can't seem to find the position of these items correctly.

anda_skoa
30th August 2014, 12:48
And where do you set the item positions?

Cheers,
_

Kamalpreet
30th August 2014, 16:24
And where do you set the item positions?

_

How do I set the item's positions? Being a newbie, I just know setPos() is to be used but I do not know how it is to implemented.

wysota
30th August 2014, 17:18
Being a newbie, I just know setPos() is to be used but I do not know how it is to implemented.

I don't see any call to setPos() in your code. And I don't think your paint() code makes any sense, especially the part where you call update(), since it makes an infinite loop of repaints.

Kamalpreet
30th August 2014, 17:28
I don't see any call to setPos() in your code. And I don't think your paint() code makes any sense, especially the part where you call update(), since it makes an infinite loop of repaints.

So how can painting in scene using mouse clicks be achieved in appropriate manner? I also want to store the positions of points painted in the scene. I am making an application similar to CAD. I am referring diagram scene example provided in the documentation. Is it appropriate to serve my purpose?

wysota
30th August 2014, 17:32
So how can painting in scene using mouse clicks be achieved in appropriate manner?
Well... it depends how you want to represent your paintings in the scene. Most direct approach would be to create items for each path drawn by the user. For that you'd have to handle events in the scene and not in the item.

Gurjot
30th August 2014, 17:43
Well... it depends how you want to represent your paintings in the scene. Most direct approach would be to create items for each path drawn by the user. For that you'd have to handle events in the scene and not in the item.

Hi,
We are taking the user's input by mouse clicks on where to draw the entities like point, line, circle, etc. So we were creating different classes for each entity.

So does that mean we should only have paint event in these entity classes and get the user inputs(mouse events) in a graphics-scene class separately?

wysota
30th August 2014, 19:04
So does that mean we should only have paint event in these entity classes and get the user inputs(mouse events) in a graphics-scene class separately?

You should have as many paint events as the scene asks you to perform. An item needs to be repainted when its state changes. Thus if you have an item representing a circle then as long as you don't modify its color, size or move it around (or move another item over it), then it won't have to be repainted.