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. Global variable/object
QtWS25 Last Chance

Global variable/object

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 583 Views
  • 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.
  • V Offline
    V Offline
    valerio.j
    wrote on last edited by
    #1

    Hello,

    I have a few functions that use the same QPixmap image, but for each function I have to initialize and set the path for each time it's used. How can I make this global so I don't have to re-declare it every time? I've tried putting it in the beggining of the header file but that didn't help. Below is an example of what I have to put in every function that uses stat_noGO.

    QPixmap stat_noGO (":/res/images/stat_noGO.png");
    
    kshegunovK 1 Reply Last reply
    0
    • V valerio.j

      Hello,

      I have a few functions that use the same QPixmap image, but for each function I have to initialize and set the path for each time it's used. How can I make this global so I don't have to re-declare it every time? I've tried putting it in the beggining of the header file but that didn't help. Below is an example of what I have to put in every function that uses stat_noGO.

      QPixmap stat_noGO (":/res/images/stat_noGO.png");
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @valerio.j
      Hi!

      How can I make this global so I don't have to re-declare it every time?

      You can't, or rather you shouldn't.

      I've tried putting it in the beggining of the header file but that didn't help.

      This will not work. The only way to do it is to have lazily initialized pointer to a heap object, but as I said, you shouldn't do that in the first place.

      QPixmap stat_noGO (":/res/images/stat_noGO.png");
      

      Use that line everywhere you need it. Don't make things global just because you feel like it - it doesn't work well in the end.

      PS.

      You can make the path to the image global if you want, this is pretty safe:

      extern const QString noGoPath;
      

      goes in the header. And in the source you have:

      const QString noGoPath = QString(":/res/images/stat_noGO.png");
      

      Then you can create the pixmaps with that path:

      QPixmap stat_noGO (noGoPath);
      

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved