[SOLVED] Qlist contents replaced with last item being inserted



  • QList with MyPoint class is replacing all previous contents with the last insertion in the list (as can be seen in the output).
    I have no idea for this behaviour. Can anybody point out the mistake or a better way to do it?
    NOTE: I have stripped down the code from the project.

    myframe.h file
    @class MyPoint
    {
    public:

    MyPoint(){mHour =0;mFilePOS= new QLine(0,0,0,0); }
    

    MyPoint( const MyPoint &faMyPoint);
    MyPoint &operator = (const MyPoint &faMyPoint);

    void drawLines();

    void setmFilePOS(QLine *faLine){mFilePOS = faLine;}
    QLine* getmFilePOS() const {return mFilePOS;}
    void setmHour(int hour){mHour = hour;}
    int getmHour() const{return mHour;}
    

    private:
    QLine *mFilePOS;
    int mHour;
    };

    class MyFrame : public QFrame
    {
    Q_OBJECT

    public:
    MyFrame( QWidget* );
    void setmLine(int x1,int y1, int x2, int y2){mLine = QLine (x1,y1,x2,y2);}
    QLine getmLine()const {return mLine;}

    private:
    ......
    ......
    QList<MyPoint *> mMyPointList ;
    QLine mLine;//Line representing a file on Frame
    QPoint *mPreviousPosition;
    QPoint *mNewPosition;
    ......
    .......
    };@

    myframe.cpp file

    @XFrame::XFrame( QWidget* parent ): QFrame( parent ), mLine(new QLine())
    {
    qRegisterMetaType<QList<MyPoint*> >("QList<MyPoint*>");
    setMouseTracking(true);
    .....
    .....
    }
    void MyFrame::drawLines()
    {
    .....
    ......
    int x1=15,y1=5,x2=15,y2=15;
    MyPoint *t1= new MyPoint();
    for (int i=0;i<20;i+=2)
    {
    mLine.setLine(x1+i, y1, x2+i, y2);
    t1->setmFilePOS(&mLine);
    mTrackPointList.append(t1);
    foreach(MyPoint *t11, mMyPointList)
    {
    qDebug()<<t11->getmFilePOS()->p1()<<t11->getmFilePOS()->p2()<<","<<mMyPointList.size()<<endl;
    }
    delete t1;
    }
    }
    }
    @
    OutPut is somethig like:

    t1: QPoint(21,5)

    QPoint(21,5) QPoint(21,15) , 1

    mLine: QLine( QPoint(35,5) , QPoint(35,15) )

    t1: QPoint(35,5)

    QPoint(35,5) QPoint(35,15) , 2

    QPoint(35,5) QPoint(35,15) , 2

    mLine: QLine( QPoint(37,5) , QPoint(37,15) )

    t1: QPoint(37,5)

    QPoint(37,5) QPoint(37,15) , 3

    QPoint(37,5) QPoint(37,15) , 3

    QPoint(37,5) QPoint(37,15) , 3

    mLine: QLine( QPoint(47,5) , QPoint(47,15) )

    t1: QPoint(47,5)

    QPoint(47,5) QPoint(47,15) , 4

    QPoint(47,5) QPoint(47,15) , 4

    QPoint(47,5) QPoint(47,15) , 4

    QPoint(47,5) QPoint(47,15) , 4


  • Lifetime Qt Champion

    Hi,

    You are appending the same t1 object to mTrackPointList at each iteration of your loop.



  • Thanks for your reply.

    I am changing contents of the t1 object in all instances.
    My problem is that all my previous list contents are replaced by the values of the last iteration. I can't understand this behavior.

    I tried creating and adding the object inside the loop (no difference). I was also deleting the object inside the loop, after adding it to my list.

    Any idea for this unwanted and understandable behavior? How can I resolve it?


  • Lifetime Qt Champion

    To be more precise, you are changing the content of mLine. And you are setting the address of mLine to t1. So practically, all t1 objects point to the same mLine object. Thus you'll always get the same value printed.



  • thanks, your answer solved my problem


  • Lifetime Qt Champion

    You're welcome !

    Since it's working now, don't forget to update the thread's title to solved so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.