Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Alternative to a global instance variable
kromacast last edited by
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:
SparkleUpdater updater( url );
WinSparkleUpdater updater( url );
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() ) );
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.
goetz last edited by
You have different options here:
create a subclass of [[Doc:QApplication]]. Add a privat pointer to an updater object and a getter to retrieve the pointer:
typedef SparkleUpdater PlatformSparkleUpdater;
typedef WinSparkleUpdater PlatformSparkleUpdater;
class SparkleApplication : public QApplication
// notice the int reference - it's IMPORTANT!!!
SparkleApplication(int & argc, char ** argv);
SparkleApplication::SparkleApplication(int & argc, char ** argv)
: QApplication(argc, argv),
Another option is to just add this to your implementation holding the slot:
extern SparkleUpdater updater;
extern WinSparkleUpdater updater;
[All brain to terminal, no guarantee to compile immediately :-) ]
kromacast last edited by
Thank you Volker. Your first suggestion works great. I appreciate your help.