[SOLVED] I can not create plugin.
-
I sampled from "echoplugin":http://qt-project.org/doc/qt-4.8/tools-echoplugin.html example to make own plugin.
My plugin Interface
@
#ifndef MODULEINTERFACE_H
#define MODULEINTERFACE_H
#include <QString>class ModuleInterface
{
public:virtual ~ModuleInterface() {}
virtual QString display(QString *);
virtual QString execute(QString *);
};QT_BEGIN_NAMESPACE
#define ModuleInterface_iid "proje.tc.ModuleInterface"
Q_DECLARE_INTERFACE(ModuleInterface,ModuleInterface_iid)
QT_END_NAMESPACE
#endif // MODULEINTERFACE_H
@
But always I got these errors.
@
/home/ckurdu/Documents/my_works/C++/qt/my_projects/qtcms-modules/menu/menu/moduleinterface.h:16: error: expected identifier before string constant
#define ModuleInterface_iid "proje.tc.ModuleInterface"/home/ckurdu/Documents/my_works/C++/qt/my_projects/qtcms-modules/menu/menu/moduleinterface.h:16: error: expected ',' or '...' before string constant
#define ModuleInterface_iid "proje.tc.ModuleInterface"/home/ckurdu/bin/Qt_5_4/5.4/gcc_64/include/QtCore/qobjectdefs_impl.h:49: error: expected constructor, destructor, or type conversion before 'namespace'
namespace QtPrivate {
^
^ ^@
My pro file
@
TEMPLATE = lib
CONFIG += plugin
QT += sql script
QT -= gui
SOURCES += menu.cpp
HEADERS += menu.h
moduleinterface.h
TARGET = $$qtLibraryTarget(qtcmsmenu)
#DESTDIR = ../plugins
#! [0]EXAMPLE_FILES = menuplugin.json
DESTDIR = /home/ckurdu/Documents/my_works/C++/qt/qtbin/qtcms_modules/menu/
@
And I can't see any generated moc files.
What is the problem?
-
Hi,
You need to either make display and execute pure virtual or give them an implementation.
On a side note, why are you passing pointers to QString ? Most of the time it's not needed. QString is one of the implicitly shared class of Qt.
-
Actually I have given them an implementation that I didn't write here. I solved the problem with changing the order of header files in real plugin file.
Firstly I included QObject and QtPlugin and after interface header file and problem disappear.I will change the pointers. Thank you SGaist.
@
#ifndef MODULEPLUGIN_H
#define MODULEPLUGIN_H
#include <QObject>
#include <QtPlugin>
#include "moduleinterface.h" // I changed location for this line.class ModulePlugin : public QObject, ModuleInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "proje.tc.ModuleInterface")
Q_INTERFACES(ModuleInterface)
public:
QString display(QString *command) Q_DECL_OVERRIDE;
QString execute(QString *command) Q_DECL_OVERRIDE;
};#endif // MODULEPLUGIN_H
@ -
Q_DECLARE_INTERFACE is defined in qobject.h included through #include <QObject> which implies that you have to write #include <QObject> before #include "moduleinterface.h" (so that Q_DECLARE_INTERFACE is defined when parsing "moduleinterface.h") in the plugin.
I would recommend including <QObject> in the interface "moduleinterface.h" to avoid arguing in the order of the includes.
Yeah, it's an old post (a referenced one), but I played again with the plugins and I struggled a little...
Below, code compliant from Qt 4.1 (I guess) to Qt 5.8 (at least)
#ifndef PLUGININTERFACE_H #define PLUGININTERFACE_H #include <QString> /** declare Q_DECLARE_INTERFACE */ #include <QObject> #define PluginInterface_iid "PluginInterface" class PluginInterface { public: virtual QString name() const = 0; virtual ~PluginInterface(){} }; Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid) #endif // PLUGININTERFACE_H
#ifndef P1_H #define P1_H #include "p1_global.h" #include "../pluginmanager/plugininterface.h" class P1SHARED_EXPORT P1 : public QObject, public PluginInterface { Q_OBJECT #if QT_VERSION >= 0x050000 Q_PLUGIN_METADATA(IID PluginInterface_iid".P1") #endif Q_INTERFACES(PluginInterface) public: virtual QString name() const; }; #endif // P1_H
#ifndef P1_GLOBAL_H #define P1_GLOBAL_H #include <QtCore/qglobal.h> #if defined(P1_LIBRARY) # define P1SHARED_EXPORT Q_DECL_EXPORT #else # define P1SHARED_EXPORT Q_DECL_IMPORT #endif #endif // P1_GLOBAL_H
#include "p1.h" #if 0x040100 <= QT_VERSION && QT_VERSION < 0x050000 #include <QtCore/qplugin.h> #endif QString P1::name() const { return QString("P1"); } #if 0x040100 <= QT_VERSION && QT_VERSION < 0x050000 Q_EXPORT_PLUGIN2(p1, P1) #endif
#------------------------------------------------- # # Project created by QtCreator 2018-03-17T08:40:29 # #------------------------------------------------- QT -= gui CONFIG += plugin TARGET = p1 TEMPLATE = lib DEFINES += P1_LIBRARY # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += p1.cpp HEADERS += p1.h\ p1_global.h