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. [Solved] How to make global access to QStringList?
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to make global access to QStringList?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 6.2k 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.
  • Z Offline
    Z Offline
    zither
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zither
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rokemoon
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            rokemoon
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • R Offline
              R Offline
              rokemoon
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • T Offline
                T Offline
                thisisbhaskar
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  rokemoon
                  wrote on last edited by
                  #8

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

                  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