Direct base 'QObject' is inaccessible due to ambiguity
-
I'm developing a
shared library(plugin) to my Qt C++ application.
Following many tutorials (like this) in the internet, I found that:
a) The plugin interface should inheritQObjectclass
b) The plugin interface MUST HAVE theQ_OBJECTmacro to enable to use the samesignalsandslotsin all developed plugins.Here is my plugin interface: (No warnings)
#ifndef PLUGIN_API_H #define PLUGIN_API_H #include <QtPlugin> #include <QString> class Plugin_API : public QObject { Q_OBJECT public: virtual ~Plugin_API() = default; virtual void showUI(void) = 0; virtual void test(void) = 0; signals: void sendString(QString string); }; Q_DECLARE_INTERFACE(Plugin_API, "com.lamar.plugin") #endif // PLUGIN_API_HHere is my plugin class.
#ifndef MYLIB_H #define MYLIB_H #include <QObject> #include <QString> #include "myLib_global.h" #include "plugin_api.h" class MYLIB_EXPORT MyLib : public QObject, public Plugin_API { Q_OBJECT Q_PLUGIN_METADATA(IID "com.lamar.plugin") Q_INTERFACES(Plugin_API) public: explicit MyLib(QObject* parent = nullptr); void test() override; void showUI() override; }; #endif // MYLIB_HIn my plugin class
myLibI got this warning message:Direct base 'QObject' is inaccessible due to ambiguityIf I remove the
QObjectinheritance, I can't use theQ_OBJECTmacro.
If I don't have both, I can't useMyLib(QObject* parent = nullptr);So, what is the right way to do that?
-
You don't need to inherit QObject in MyLib because Plugin_API already has.
So MyLib already inherits QObject even without that "public QObject".
Just like when we subclass QWidget, we don't need to inherit QObject in our code, but the new class can use Q_OBJECT macro and signals/slots, because QWidget inherits QObject.