Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Can I (dynamically) call an arbitrary plugin class function from another class?
primem0ver last edited by
My goal (if it is possible)... is to create a widget that inherits QToolButton and is assigned a callback function from a plugin. I know that it is possible to create a function pointer within the Button class but the problem is that from what I have read about function pointers, the class type is part of the declaration. For example:
@MyReturnType (MyClass::* functionPointer)(ParameterType1 parameter1, ParameterType2 parameter2,...)@
As I understand it, In the above example, the type MyClass must be known ahead of time but since this is a plugin, that knowledge is not possible. Since the plugin interface is the only thing known a priori (ahead of time) and a plugin will have an arbitrary class name and since it can have (and will have) any number of unique functions that need to be called when a button is pressed, it is impossible to know what MyClass is ahead of time.
So is doing such a thing possible? I was thinking that the signal slot features of Qt might be a solution. Theoretically I could have a signal that uses a function pointer as a parameter for the signal and is passed to the slot. The slot would exist in the target plugin and the target plugin could then call the function that needs to be called from the slot. But... I might run into the same problem as above if the syntax for such a feat would again require that MyClass be known ahead of time.
Ultimately this Button type I am proposing is to be used in a universal toolbar type because each of these plugins must provide a toolbar that is used to give the user access to the functionality of the plugin.
Perhaps there is a simpler solution?
giesbert last edited by
what you can do is use the SIGNAL/SLOT mechanism for that. If the plugin is a QObject derived class, you can call functions there by "QMetaObject::invokeMethod":http://qt-project.org/doc/qt-5/qmetaobject.html#invokeMethod