Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] Qlist contents replaced with last item being inserted

    General and Desktop
    2
    6
    1243
    Loading More Posts
    • 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
      tesmai4 last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • T
          tesmai4 last edited by

          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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 Reply Quote 0
            • T
              tesmai4 last edited by

              thanks, your answer solved my problem

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • First post
                  Last post