Alternative to a global instance variable



  • Hello All,

    I am currently developing a cross-platform application in which I am implementing SparkleUpdater for Mac and WinSparkleUpdater for Windows. What I am hoping to do is provide a way to initiate a check for updates from a separate project.

    For clarity, this is how my application is currently set up:

    I have a Qt project set to be the startup project which will startup the whole application. In that project I have main.cpp which includes the following:

    @#ifdef Q_OS_MAC
    SparkleUpdater updater( url );
    #endif
    #ifdef Q_OS_WIN
    WinSparkleUpdater updater( url );
    #endif@

    SparkleUpdater and WinSparkleUpdater are just interface classes that access the real thing depending on the OS. Both have access to QApplication::applicationName() and all other necessary information to function.

    The startup project depends on another project that provides basic support for the application. One such support is the creation of the SystemTray icon menu. To this menu I would like to create a trigger that will trigger the manual update function of WinSparkle or Sparkle. I need it to access the existing instance of updater but I cannot pass updater as a parameter.

    Something like this would be great if I can do it:
    @
    m_systemTrayIcon = new QSystemTrayIcon();
    ...
    m_systemTrayMenu->addAction( tr("Check For Updates"), this, SLOT( checkForUpdates() ) );
    ...
    m_systemTrayIcon->show();
    @

    ...

    @
    checkForUpdates()
    {
    updater.checkForUpdates();
    }
    @

    I also am unable to move the WinSparkleUpdater and SparkleUpdater classes to the supporting project because the supporting project is used in other applications and I would like to have the updating take place with just the startup project.

    I have investigated a few different strategies for accomplishing this sort of process but I am not able to find anything that provides a solution. Perhaps, I am just unaware of the correct terminology to express the correct Google search. :)

    If anyone here could give me some direction or even a Qt specific method of accomplishing this functionality, I would really appreciate it.

    Thanks in advance for your responses and feel free to ask if additional clarification is needed.



  • You have different options here:

    subclass QApplication
    create a subclass of [[Doc:QApplication]]. Add a privat pointer to an updater object and a getter to retrieve the pointer:

    @
    #if defined(Q_OS_MACX)
    typedef SparkleUpdater PlatformSparkleUpdater;
    #elif defined(Q_OS_WIN32)
    typedef WinSparkleUpdater PlatformSparkleUpdater;
    #endif

    class SparkleApplication : public QApplication
    {
    O_OBJECT
    public:
    // notice the int reference - it's IMPORTANT!!!
    SparkleApplication(int & argc, char ** argv);
    ~SparkleApplication();

    PlatformSparkleUpdater* updater();
    

    private:
    PlatformSparkleUpdater *m_updater;
    }

    // implemenation:

    SparkleApplication::SparkleApplication(int & argc, char ** argv)
    : QApplication(argc, argv),
    m_updater(new PlatformSparkleUpdater)
    {
    }

    PlatformSparkleUpdater* SparkleApplication::updater()
    {
    return m_updater;
    }
    @

    Another option is to just add this to your implementation holding the slot:

    @
    #if defined(Q_OS_MACX)
    extern SparkleUpdater updater;
    #elif defined(Q_OS_WIN32)
    extern WinSparkleUpdater updater;
    #endif
    @

    [All brain to terminal, no guarantee to compile immediately :-) ]



  • Thank you Volker. Your first suggestion works great. I appreciate your help.


Log in to reply
 

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