[Solved] How to make global access to QStringList?



  • @#ifndef GLOBAL_H
    #define GLOBAL_H
    #include <QStringList>

    class global{
    public:
    QStringList List;
    global();
    };

    global::global(){
    List<<"List1","List2","List3";
    }@

    I make this class to access List from any class that includes "global.h"
    But, I don't want to create like that

    @global myGlobal;
    QStringList myList = myGlobal.List;@

    I would like to make direct access to QStringList without creating class.
    How can I do that?

    Thanks



  • Don't know what is your use-case, you can have a static QStringList and a static method to return it.



  • That QStringList contains common values for all other class in projects.

    So, I make a common place & share this QStringList. But I don't like to place this QStringList inside a class. Because it need to make instance of that class to call this variable.

    I would like to make globally accessible without need to make an instance of class. I try it but I can't fill QString into List without placing QStringList into the class.

    Thanks



  • Maybe this will help you:
    @static QStringList list(QStringList() << "List1" << "List2" << "List3");@



  • or you can use your class and create an object in this header
    @static global myGlobal;@
    and in another source you can use as you wrote
    @QStringList myList = myGlobal.List;@
    [Update] use static for one initialization



  • or like that :-)
    @#ifndef GLOBAL_H
    #define GLOBAL_H
    #include <QStringList>

    class Global
    {
    public:
    Global();

    QStringList List;
    

    } myGlobal;

    Global::Global(){
    List<<"List1","List2","List3";
    }
    #endif GLOBAL_H@

    @QStringList myList = myGlobal.List;@



  • [quote author="rokemoon" date="1313060653"]or like that :-)
    @#ifndef GLOBAL_H
    #define GLOBAL_H
    #include <QStringList>

    class Global
    {
    public:
    Global();

    QStringList List;
    

    } myGlobal;

    Global::Global(){
    List<<"List1","List2","List3";
    }
    #endif GLOBAL_H@

    @QStringList myList = myGlobal.List;@[/quote]

    My be this is not the best way to do it. This creates one Global object each time this .h file is included in a .cpp file. But that is not zither wants.



  • [quote author="Vijay Bhaska Reddy" date="1313061410"]
    My be this is not the best way to do it. This creates one Global object each time this .h file is included in a .cpp file. But that is not zither wants.
    [/quote]
    Oh really, I don't think about this. Thanks.


Log in to reply
 

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