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 .


    static  QList<QString> component_names;


    class Example 

    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;
    if(! | QIODevice::Append))
            doDebug("unable to open the file ");
    QTextStream out(&mapFile);
    out<<;   //getting error "undefined reference to Example::component_names "
    out<<;     //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

  • Moderators



    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
    QList<QString> component_names;

    than from the other class, you access it this way


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

    #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);
        globalall = new GlobalAll();
        MainWindow w;
        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 ! ))

  • Lifetime Qt Champion

    @Taz742 I really hope he will not do that!
    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?

  • Lifetime Qt Champion

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

  • Lifetime Qt Champion

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

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

    [edit: fixed missing variable call in doSomething SGaist]

  • Lifetime Qt Champion

    @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