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. Q_GLOBAL_STATIC can't find created varibale after placing macro in cpp

Q_GLOBAL_STATIC can't find created varibale after placing macro in cpp

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 443 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.
  • M Offline
    M Offline
    Marioz
    wrote on last edited by
    #1

    I have a class that needs to have one object and accessed from multiple widgets (classes) in my project.
    I was afraid singleton isn't thread-safe enough so I found the Q_GLOBAL_STATIC macro.
    After adding the macro to the .h file of the class , I can use it from anywhere but seems that every time I inlclude the .h it basically creates another instance (and yes , I read that in documentation it says "More importantly, this macro should be placed in source files, never in headers. Since the resulting object is has static linkage, if the macro is placed in a header and included by multiple source files, the object will be defined multiple times and will not cause linking errors. ... "
    However , when I add the macro to the .CPP other classes don't seem to find the variable :
    'variable' was not declared in this scope.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Marioz said in Q_GLOBAL_STATIC can't find created varibale after placing macro in cpp:

      Q_GLOBAL_STATIC

      This is meant for global statics used in one translation unit, not for singletons distributed over the whole code. Singletons should be avoided and are never the correct solution for a problem.

      I have a class that needs to have one object and accessed from multiple widgets (classes) in my project.

      Then pass a pointer to this class to the other classes.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Marioz said in Q_GLOBAL_STATIC can't find created varibale after placing macro in cpp:

        Q_GLOBAL_STATIC

        This is meant for global statics used in one translation unit, not for singletons distributed over the whole code. Singletons should be avoided and are never the correct solution for a problem.

        I have a class that needs to have one object and accessed from multiple widgets (classes) in my project.

        Then pass a pointer to this class to the other classes.

        M Offline
        M Offline
        Marioz
        wrote on last edited by
        #3

        Then pass a pointer to this class to the other classes.

        This was my very first approach but the problem is the huge number of widgets that are added.
        An average session would require adding more than 30 widgets(instances) of 8 classes.
        Is there any other way to make singleton-like approach without the risk of further troubles ?

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why do you need a singleton in the first place? There is no need for it at all.
          And creating a singleton in a cpp file and giving access to them from outside should be that hard for a c programmer...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          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