Access QLIST from other class



  • 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



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



  • it is defined in the header file
    example.h



  • @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");
    


  • @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 ! ))


  • Moderators

    @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.



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


  • Moderators

    @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.



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


  • Moderators

    @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]


  • Moderators

    @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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.