Qt Forum

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

    Forum Updated on Feb 6th

    [Solved] QList append every other object

    General and Desktop
    3
    5
    962
    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.
    • S
      sting last edited by

      I have a QList and when I attempt to append instances to it, it will only succeed every other attempt.

      in mainwindow.h
      @private:
      QList<SimDevice*> deviceList;

      void addDevice(QString name);
      

      @
      in mainWindow.cpp
      @
      deviceList = QList<SimDevice*>(); //DeviceList();
      @ and
      @
      void MainWindow::addDevice(QString deviceName){
      deviceList.append(new SimDevice(deviceName));
      }
      @
      The SimDevice class
      @
      class SimDevice
      {
      public:
      SimDevice(QString name);
      ~SimDevice();
      QString getDeviceName();
      private:
      QString deviceName;
      };
      @
      And when I add elements and check on them
      @
      addDevice("bar");
      addDevice("foo");
      addDevice("junk");
      addDevice("trash");
      addDevice("stuff");
      for (int i = 0; i < deviceList.count(); i++){
      SimDevice *device = deviceList.takeAt(i);
      QString name = device->getDeviceName();
      qDebug() << name;
      }
      @
      the result is
      Starting /home/ray/Qt/projects/build-SimController-Desktop_Qt_5_3_GCC_64bit-Debug/SimController...
      "bar"
      "junk"
      "stuff"
      /home/ray/Qt/projects/build-SimController-Desktop_Qt_5_3_GCC_64bit-Debug/SimController exited with code 0

      1 Reply Last reply Reply Quote 0
      • S
        sting last edited by

        Here is a minimal example. What am I doing wrong?

        @
        MainWindow::MainWindow(QWidget parent)
        : QMainWindow(parent)
        {
        deviceList = QList<SimDevice
        >(); //DeviceList();
        addDevice("bar");
        addDevice("foo");
        addDevice("junk");
        addDevice("trash");
        addDevice("stuff");

        for (int i = 0; i < deviceList.count(); i++){
            SimDevice *device = deviceList.takeAt(i);
            QString name = device->getDeviceName();
            qDebug() << name;
        }
        

        }

        void MainWindow::addDevice(QString deviceName){
        deviceList.append(new SimDevice(deviceName));
        }@
        @
        class SimDevice
        {
        public:
        SimDevice(QString name);
        ~SimDevice();
        QString getDeviceName();
        private:
        QString deviceName;
        };
        @
        @
        SimDevice::SimDevice(QString name)
        {
        deviceName = name;
        }

        QString SimDevice::getDeviceName(){
        return deviceName;
        }@

        Starting /home/ray/build-TestQList-Desktop_Qt_5_3_GCC_64bit-Debug/TestQList...
        "bar"
        "junk"
        "stuff"
        /home/ray/build-TestQList-Desktop_Qt_5_3_GCC_64bit-Debug/TestQList exited with code 0

        1 Reply Last reply Reply Quote 0
        • JKSH
          JKSH Moderators last edited by

          Hi,

          Use at() instead of takeAt(). takeAt() removes the item from your list, which shifts the positions of all the other items in your list.

          P.S. Remember to delete your SimDevice objects when you don't need them anymore.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply Reply Quote 0
          • A
            andreyc last edited by

            A note that is not related to your problem
            You don't need this line in your MainWindow constructor
            @
            deviceList = QList<SimDevice*>(); //DeviceList();
            @

            1 Reply Last reply Reply Quote 0
            • S
              sting last edited by

              JKSH: Thanks, I guess I should have RTFM. I just used takeAt in another area, I did need to remove the element, but I spaced on that fine point. Thank you

              andreyc: Thanks for the tip.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post