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. Weird behavior of QList<T>
Forum Updated to NodeBB v4.3 + New Features

Weird behavior of QList<T>

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 4 Posters 6.4k Views 1 Watching
  • 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.
  • jsulmJ jsulm

    @advtaco Are you sure you really have roomCount entries in the list? You should check that first.

    A Offline
    A Offline
    advtaco
    wrote on last edited by
    #13

    @jsulm Yep, i checked that in the debugger

    @VRonin That's this line:

     printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data());
    
    jsulmJ 1 Reply Last reply
    0
    • VRoninV VRonin

      @advtaco said in Weird behavior of QList<T>:

      That's the stack trace

      Which one is line 55 of cmdhandler.cpp?

      I know this is something unbelievably obvious that I'm missing but I just can't see the bug yet

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #14

      @VRonin @jsulm

      @advtaco said in Weird behavior of QList<T>:

      #ifndef KROOM_H
      #define KROOM_H

      #include <QList>
      #include <QString>
      #include <kswitch.h>
      #include <kblind.h>

      class kRoom
      {
      public:
      kRoom(QString name);
      QList<kSwitch> switches;
      QList<kBlind> blinds;
      QString name;
      void addSwitch(kSwitch newSwitch);
      void removeSwitch(int remSwitch);
      void addBlind(kBlind newBlind);
      void removeBlind(int remBlind);
      };

      When I used class in other classes I had a problem with the following:
      like this

      > #include <QList>
      > #include <QString>
      > #include <kswitch.h>
      > #include <kblind.h>
      
      class kSwtich;
      class kBlind;
      

      Could this be a problem anyway?

      Do what you want.

      jsulmJ 1 Reply Last reply
      0
      • Taz742T Taz742

        @VRonin @jsulm

        @advtaco said in Weird behavior of QList<T>:

        #ifndef KROOM_H
        #define KROOM_H

        #include <QList>
        #include <QString>
        #include <kswitch.h>
        #include <kblind.h>

        class kRoom
        {
        public:
        kRoom(QString name);
        QList<kSwitch> switches;
        QList<kBlind> blinds;
        QString name;
        void addSwitch(kSwitch newSwitch);
        void removeSwitch(int remSwitch);
        void addBlind(kBlind newBlind);
        void removeBlind(int remBlind);
        };

        When I used class in other classes I had a problem with the following:
        like this

        > #include <QList>
        > #include <QString>
        > #include <kswitch.h>
        > #include <kblind.h>
        
        class kSwtich;
        class kBlind;
        

        Could this be a problem anyway?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #15

        @Taz742 Sory, I don't follow: what problem do you mean?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Taz742T 1 Reply Last reply
        0
        • A advtaco

          @jsulm Yep, i checked that in the debugger

          @VRonin That's this line:

           printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data());
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #16

          @advtaco Not related to your question: may I ask you why you use printf C function? It makes your code more complex as you need to get char* out of QString. Why not use qDebug for debugging or std::cout for regular std out - you're using C++ not C :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          VRoninV 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Taz742 Sory, I don't follow: what problem do you mean?

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by
            #17

            @jsulm undefined refernce (class name).
            @advtaco you are using Qt 5.9 yeap?
            please check other version if you can.

            Do what you want.

            jsulmJ 1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #18

              I'm speechless... I can only thing of something weird going on in building

              ok, let's try with an alternative.
              replace

              for (int i = 0; i < roomCount; i++)
                 {
                     printf(QString::number(i).toLatin1().data());
                     printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data());
                     printf(QString("Switches: " + QString::number(building->rooms.at(i).switches.count()) + "\n\r").toLatin1().data());
                     printf(QString("Blinds: " + QString::number(building->rooms.at(i).blinds.count()) + "\n\r").toLatin1().data());
                     printf("+---------------------------+\n\r");
                 }
              

              with

              foreach(const kRoom& singleRoom, building->rooms){
              qDebug() << singleRoom.name;
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              A 1 Reply Last reply
              2
              • Taz742T Taz742

                @jsulm undefined refernce (class name).
                @advtaco you are using Qt 5.9 yeap?
                please check other version if you can.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #19

                @Taz742 Well, you need to have the class definition somewhere, a forward declaration isn't a definition.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @advtaco Not related to your question: may I ask you why you use printf C function? It makes your code more complex as you need to get char* out of QString. Why not use qDebug for debugging or std::cout for regular std out - you're using C++ not C :-)

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #20

                  @jsulm said in Weird behavior of QList<T>:

                  @advtaco Not related to your question: may I ask you why you use printf C function? It makes your code more complex as you need to get char* out of QString. Why not use qDebug for debugging or std::cout for regular std out - you're using C++ not C :-)

                  For more infos on this front, see https://stackoverflow.com/questions/3886105/how-to-print-to-console-when-using-qt

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • VRoninV VRonin

                    I'm speechless... I can only thing of something weird going on in building

                    ok, let's try with an alternative.
                    replace

                    for (int i = 0; i < roomCount; i++)
                       {
                           printf(QString::number(i).toLatin1().data());
                           printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data());
                           printf(QString("Switches: " + QString::number(building->rooms.at(i).switches.count()) + "\n\r").toLatin1().data());
                           printf(QString("Blinds: " + QString::number(building->rooms.at(i).blinds.count()) + "\n\r").toLatin1().data());
                           printf("+---------------------------+\n\r");
                       }
                    

                    with

                    foreach(const kRoom& singleRoom, building->rooms){
                    qDebug() << singleRoom.name;
                    }
                    
                    A Offline
                    A Offline
                    advtaco
                    wrote on last edited by
                    #21

                    @VRonin The qDebug-Variant works! Still don't understand why the old version doesn't but at least it does something now!

                    @jsulm @VRonin I am looking into other methods to output now

                    @Taz742 Yes, I am using Qt 5.9

                    Taz742T 1 Reply Last reply
                    0
                    • A advtaco

                      @VRonin The qDebug-Variant works! Still don't understand why the old version doesn't but at least it does something now!

                      @jsulm @VRonin I am looking into other methods to output now

                      @Taz742 Yes, I am using Qt 5.9

                      Taz742T Offline
                      Taz742T Offline
                      Taz742
                      wrote on last edited by
                      #22

                      @advtaco
                      The problem was qDebug () or foreach (const kRoom & singleRoom, building-> rooms) ?

                      Do what you want.

                      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