McNumber
McNumber

Reputation: 177

Using global variables of main program in Qt plugin

I have a Qt application, that uses global variables as pointers to some singleton service objects (I know this is not the best solution). Now I want to make my application extendable with Qt plugins. Can I use global variables of the main program in the plugin? Or do Qt plugins have a separate address space?

Upvotes: 1

Views: 1185

Answers (3)

Naszta
Naszta

Reputation: 7744

Allocate your variable memory space to a QSharedMemory object. QSharedMemory frees up, when all connected threads and processes has been finished. If you want use separated memory for all of your processes, you should use process id as the name of QSharedMemory object.

Upvotes: 0

Bruce
Bruce

Reputation: 7132

One of the convenient (but not so clean) way is to use properties on your application. In the main component, you push a pointer to this global to your "main application":

qApp->setProperty("StringProperty",qVariantFromValue(qobject_cast<QObject*>(this )));          

In your plugin, you can retrieve it with something like that:

QObject* pMyobject = qApp->property("StringProperty").value<QObject*>(); 

Upvotes: 1

harper
harper

Reputation: 13690

Your plug in (.so or .DLL) will run in the same address space. You need some means in your plug-in architecture to provide the address of the main variables to the plug ins.

Upvotes: 1

Related Questions