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. Access QLIST from other class
Qt 6.11 is out! See what's new in the release blog

Access QLIST from other class

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 5.3k 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.
  • A Offline
    A Offline
    akshay123
    wrote on last edited by
    #1

    Hi all ,

    i am declaring the a qt list in one class and pushing the values in the qtlist . this list i want to access in the other class .

    Example.h

    static  QList<QString> component_names;
    

    Examle.cpp

    class Example 
    {
    component_names.append("one");
    component_names.append("two");
    }
    

    Another.h //Another class where i want to access the qlist

    #include < Example.h>
    

    Another.cpp //Another class where i want to access the qlist

    class another
    {
    QFile mapFile;
    mapFile.setFileName("GenMapFile.map");
    if(!mapFile.open(QIODevice::WriteOnly | QIODevice::Append))
        {
            doDebug("unable to open the file ");
        }
    QTextStream out(&mapFile);
    
    out<<Example::component_names.at(0);   //getting error "undefined reference to Example::component_names "
    out<<Example::component_names.at(1);     //getting error "undefined reference to Example::component_names "
    
    }
    

    pls hep to solve this issue

    FlotisableF J.HilkJ Taz742T 3 Replies Last reply
    0
    • A akshay123

      Hi all ,

      i am declaring the a qt list in one class and pushing the values in the qtlist . this list i want to access in the other class .

      Example.h

      static  QList<QString> component_names;
      

      Examle.cpp

      class Example 
      {
      component_names.append("one");
      component_names.append("two");
      }
      

      Another.h //Another class where i want to access the qlist

      #include < Example.h>
      

      Another.cpp //Another class where i want to access the qlist

      class another
      {
      QFile mapFile;
      mapFile.setFileName("GenMapFile.map");
      if(!mapFile.open(QIODevice::WriteOnly | QIODevice::Append))
          {
              doDebug("unable to open the file ");
          }
      QTextStream out(&mapFile);
      
      out<<Example::component_names.at(0);   //getting error "undefined reference to Example::component_names "
      out<<Example::component_names.at(1);     //getting error "undefined reference to Example::component_names "
      
      }
      

      pls hep to solve this issue

      FlotisableF Offline
      FlotisableF Offline
      Flotisable
      wrote on last edited by
      #2

      @akshay123
      is component_names defined in the class Example ? or in the global scope?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        akshay123
        wrote on last edited by
        #3

        it is defined in the header file
        example.h

        1 Reply Last reply
        0
        • A akshay123

          Hi all ,

          i am declaring the a qt list in one class and pushing the values in the qtlist . this list i want to access in the other class .

          Example.h

          static  QList<QString> component_names;
          

          Examle.cpp

          class Example 
          {
          component_names.append("one");
          component_names.append("two");
          }
          

          Another.h //Another class where i want to access the qlist

          #include < Example.h>
          

          Another.cpp //Another class where i want to access the qlist

          class another
          {
          QFile mapFile;
          mapFile.setFileName("GenMapFile.map");
          if(!mapFile.open(QIODevice::WriteOnly | QIODevice::Append))
              {
                  doDebug("unable to open the file ");
              }
          QTextStream out(&mapFile);
          
          out<<Example::component_names.at(0);   //getting error "undefined reference to Example::component_names "
          out<<Example::component_names.at(1);     //getting error "undefined reference to Example::component_names "
          
          }
          

          pls hep to solve this issue

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @akshay123

          Hi,

          first off, let me say this:

          Don't do it!

          Accessing an class specific item from an other class will lead very quickly to Spaghetti code

          That said, its super easy to do.

          In the class that has the QList, define it as public:

          //in class.h
          public:
          QList<QString> component_names;
          

          than from the other class, you access it this way

          MyClassReferenz->component_names.append("one");
          MyClassReferenz->component_names.append("two");
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          3
          • A akshay123

            Hi all ,

            i am declaring the a qt list in one class and pushing the values in the qtlist . this list i want to access in the other class .

            Example.h

            static  QList<QString> component_names;
            

            Examle.cpp

            class Example 
            {
            component_names.append("one");
            component_names.append("two");
            }
            

            Another.h //Another class where i want to access the qlist

            #include < Example.h>
            

            Another.cpp //Another class where i want to access the qlist

            class another
            {
            QFile mapFile;
            mapFile.setFileName("GenMapFile.map");
            if(!mapFile.open(QIODevice::WriteOnly | QIODevice::Append))
                {
                    doDebug("unable to open the file ");
                }
            QTextStream out(&mapFile);
            
            out<<Example::component_names.at(0);   //getting error "undefined reference to Example::component_names "
            out<<Example::component_names.at(1);     //getting error "undefined reference to Example::component_names "
            
            }
            

            pls hep to solve this issue

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

            @akshay123
            Hi.
            Your list will not be available for every class, if it will not be globally preceding.
            Let's create a new class in which the variables or store it on our lists.

            Add new File C++, c++ class named globalall.

            #ifndef GLOBALALL_H
            #define GLOBALALL_H
            
            #include <QObject>
            #include "user.h"
            #include "QSqlDatabase"
            #include "olduser.h"
            #include "QFile"
            #include "QXmlStreamReader"
            #include "QMap"
            
            class GlobalAll
            {
            public:
                GlobalAll();
                QMap<QString, QString> my_map;
                User* LoggedUser;
                OldUser* Olduser;
                QSqlDatabase db;
                bool IndicatorCurChange;
                //int lastwidget;
            };
            
            #endif // GLOBALALL_H
            

            Constructor does not want anything.

            Now Create One Header File.
            Add New C++, c++ header file. named globaldefines.

            #ifndef GLOBALDEFINES_H
            #define GLOBALDEFINES_H
            
            #include "globalall.h"
            #include "user.h"
            #include "QDebug"
            
            extern GlobalAll* globalall;
            
            #endif // GLOBALDEFINES_H
            

            GlobalAll* globalall; <-- we do not want it public, It should be extern.

            Thus we announced Globalall global classes.

            Now Go main.cpp

            #include "mainwindow.h"
            #include <QApplication>
            #include "userlogindlg.h"
            #include "user.h"
            //#include "globalall.h"
            #include "globaldefines.h"
            #include "QDebug"
            
            GlobalAll* globalall;
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
            
                QLocale::setDefault(QLocale::English);
            
                globalall = new GlobalAll();
            
                MainWindow w;
            
                w.show();
            
                return a.exec();
            }
            

            GlobalAll * globalall it is necessary to be above!!!

            Everything that is all :)) sorry for English I do not have it.

            I hope you'll do that ! ))

            Do what you want.

            jsulmJ 1 Reply Last reply
            0
            • Taz742T Taz742

              @akshay123
              Hi.
              Your list will not be available for every class, if it will not be globally preceding.
              Let's create a new class in which the variables or store it on our lists.

              Add new File C++, c++ class named globalall.

              #ifndef GLOBALALL_H
              #define GLOBALALL_H
              
              #include <QObject>
              #include "user.h"
              #include "QSqlDatabase"
              #include "olduser.h"
              #include "QFile"
              #include "QXmlStreamReader"
              #include "QMap"
              
              class GlobalAll
              {
              public:
                  GlobalAll();
                  QMap<QString, QString> my_map;
                  User* LoggedUser;
                  OldUser* Olduser;
                  QSqlDatabase db;
                  bool IndicatorCurChange;
                  //int lastwidget;
              };
              
              #endif // GLOBALALL_H
              

              Constructor does not want anything.

              Now Create One Header File.
              Add New C++, c++ header file. named globaldefines.

              #ifndef GLOBALDEFINES_H
              #define GLOBALDEFINES_H
              
              #include "globalall.h"
              #include "user.h"
              #include "QDebug"
              
              extern GlobalAll* globalall;
              
              #endif // GLOBALDEFINES_H
              

              GlobalAll* globalall; <-- we do not want it public, It should be extern.

              Thus we announced Globalall global classes.

              Now Go main.cpp

              #include "mainwindow.h"
              #include <QApplication>
              #include "userlogindlg.h"
              #include "user.h"
              //#include "globalall.h"
              #include "globaldefines.h"
              #include "QDebug"
              
              GlobalAll* globalall;
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  QLocale::setDefault(QLocale::English);
              
                  globalall = new GlobalAll();
              
                  MainWindow w;
              
                  w.show();
              
                  return a.exec();
              }
              

              GlobalAll * globalall it is necessary to be above!!!

              Everything that is all :)) sorry for English I do not have it.

              I hope you'll do that ! ))

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

              @Taz742 I really hope he will not do that!
              DO NOT USE GLOBAL VARIABLES!
              Global variables are a source for troubles and they usually are not needed.

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

              Taz742T 1 Reply Last reply
              3
              • jsulmJ jsulm

                @Taz742 I really hope he will not do that!
                DO NOT USE GLOBAL VARIABLES!
                Global variables are a source for troubles and they usually are not needed.

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

                @jsulm
                So, how are you going to your List values found everywhere?

                Do what you want.

                jsulmJ 1 Reply Last reply
                0
                • Taz742T Taz742

                  @jsulm
                  So, how are you going to your List values found everywhere?

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

                  @Taz742 I'm quite sure he does not need access to this list EVERYWHERE in his app.
                  If he does, then he should think about his software design first. Why does he need to access it everywhere?
                  How to access it? Well, get the instance of the class and access it directly (if it is public) or via a public getter.
                  How to get instance depends: the class can be a singleton (not a very good solution either), dependency injection, ...
                  One more possibility: this list could be static class member if all instances share the same list.

                  So, the question is: who needs the access? First answer this question and then think about how to provide the access.

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

                  Taz742T 1 Reply Last reply
                  2
                  • jsulmJ jsulm

                    @Taz742 I'm quite sure he does not need access to this list EVERYWHERE in his app.
                    If he does, then he should think about his software design first. Why does he need to access it everywhere?
                    How to access it? Well, get the instance of the class and access it directly (if it is public) or via a public getter.
                    How to get instance depends: the class can be a singleton (not a very good solution either), dependency injection, ...
                    One more possibility: this list could be static class member if all instances share the same list.

                    So, the question is: who needs the access? First answer this question and then think about how to provide the access.

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

                    @jsulm
                    It would be nice if you show us how to do it.

                    Do what you want.

                    jsulmJ 2 Replies Last reply
                    0
                    • Taz742T Taz742

                      @jsulm
                      It would be nice if you show us how to do it.

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

                      @Taz742 Well, as I said there are many possibilities. For sure I will not show all of them.
                      Here is one:

                      class A
                      {
                          public:
                              QList<int> list;
                      };
                      class B
                      {
                          public:
                              B(const A* a_): a(a_) {}
                              void doSomething() { a->list.append(10); }
                          private:
                              A *a;
                      };
                      class MainWindow: public QMainWindow
                      {
                          public:
                              MainWindow()
                             {
                                 a = new A();
                                 b = new B(a);
                                 b->doSomething();
                             }
                          private:
                              A *a;
                              B *b;
                      };
                      

                      [edit: fixed missing variable call in doSomething SGaist]

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

                      1 Reply Last reply
                      4
                      • Taz742T Taz742

                        @jsulm
                        It would be nice if you show us how to do it.

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

                        @Taz742 What I wrote was not critique on you! I just want to point out the importance of good software design and excessive usage of global variables is a sign of bad design.

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

                        Taz742T 1 Reply Last reply
                        4
                        • jsulmJ jsulm

                          @Taz742 What I wrote was not critique on you! I just want to point out the importance of good software design and excessive usage of global variables is a sign of bad design.

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

                          @jsulm
                          Yes, I understand with you.
                          I'm not good with the classes, Recently I started learning.. thank you.

                          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