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 applicat开发者_Go百科ion 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?
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.
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*>();
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.
精彩评论