Global variable/object



  • 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");
    

  • Qt Champions 2016

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


Log in to reply
 

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