Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

One header file to be used only once in whole program



  • I have header file named
    simple.h
    Suppose with messageBox and one variable calledVariable fetched from database in constructor
    (THIS VARIABLE IS NEVER GONNA CHANGE)

    It is used in another header file
    name.h

    And this header file is used in many files

    #include"simple.h"
    

    In 10 files.

    Here when I do this I get 10 message box with same thing. What I want is to
    call simple.h ONLY once. and get the same variable value calledVariable which we will get in the first execution


  • Lifetime Qt Champion

    @Thank-You Declare the variable es extern in the header file and define it once somewhere in a C++ file.
    See https://en.cppreference.com/w/cpp/language/storage_duration for details.
    But keep in mind that QObject based classes are not allowed to be instantiated before QApplication instance is created!


Log in to reply