-1

I have two classes: point and mainwindow. I declared pointer of point class in mainwindow.h as:

point *item;

I create an object of point class in mainwindow.cpp:

void MainWindow::drawPoint(){
    item = new point;
    scene->addItem(item);
}

This function enables painting of point in qgraphicsscene.

There is a vector that stores QPointF values in point class. The storage class is the type of vector.

QVector<storage> point_vector;

set_point() is defined in storage class that sets the value of point p1.

storage store_point;
store_point.set_point(p1);
point_vector.push_back(store_point);

On iterating the vector in mainwindow.cpp,

for(it = item->point_vector.begin(); it != item->point_vector.end(); it++)
{
    qDebug() << "size" << item->point_vector.size() << "\n";
}

Even though I create multiple points in the scene, the size of vector always remains 1.

I think this might be happening because every time the function drawPoint() is called, the previous object of point class is over-ridden.

I am unable to figure out how to resolve this issue. Any help will be appreciated.

Relevant Code

http://www.paste.org/74408

Kamalpreet Grewal
  • 822
  • 1
  • 13
  • 28

1 Answers1

0

Your code is spread out too much, you should try creating proper classes...

As far as I can see, the "point_vector" that gets pushed back items is a local variable declared / defined in point.cpp

You did not post point.h file, but I am guessing that it has point_vector defined as part of the class point... But you are not using the point_vector member variable in the cpp, you are using the locally declared one

It would be easier to see if, like the comments on your question say, you posted a more complete portion of the code.

Thalia
  • 13,637
  • 22
  • 96
  • 190