Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Qlist contents replaced with last item being inserted
QtWS25 Last Chance

[SOLVED] Qlist contents replaced with last item being inserted

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tesmai4
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tesmai4
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tesmai4
            wrote on last edited by
            #5

            thanks, your answer solved my problem

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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 :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved