I'd like to avoid using qmake
and .pro
files. The problem is that I cannot get cmake
to work nicely with Qt Plugins
. I've included the code below showing the interface, plugin, loader that work correctly for the given .pro
file but I cannot figure out how to transfer this functionality to cmake
.
Plugin Interface
Pure virtual interface, known by the loader.
#include <QtCore/qglobal.h>
class HelloPluginInterface
{
public:
virtual void DoSomething() const = 0;
};
Q_DECLARE_INTERFACE( HelloPluginInterface, "com.harbrick.Qt.HelloPluginInterface")
Plugin
Plugin that becomes a .so
to be loaded by the loader.
#include <QtPlugin>
#include "helloplugin_global.h"
class HelloPlugin : public QObject, public HelloPluginInterface
{
Q_OBJECT
Q_PLUGIN_METADATA( IID "com.harbrick.Qt.HelloPluginInterface" )
Q_INTERFACES( HelloPluginInterface )
public:
void DoSomething() const;
};
Plugin Loader
void MainWindow::LoadPlugin( const QString& pathToLibrary )
{
QPluginLoader loader( pathToLibrary );
QObject* instance = loader.instance();
if( instance )
{
HelloPluginInterface *plugin = qobject_cast< HelloPluginInterface* >( instance );
if(plugin)
{
//do stuff
...
}
else
{
qDebug() << "Not a plugin: " << Filename << " = " << loader.errorString();
}
}
}
CMakeLists.txt
Can't get to work
project( HelloPlugin )
cmake_minimum_required( VERSION 2.8.11 )
set( CMAKE_INCLUDE_CURRENT_DIR ON )
find_package(Qt5Widgets)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
)
set( INCLUDE
cmakeplugin.h
cmakeplugininterface.h
)
set( SOURCES
cmakeplugin.cpp
)
add_executable(${PROJECT_NAME} ${INCLUDE} ${SOURCES} ${SRC_LIST})
add_library( cmakePlugin SHARED cmakeplugin.cpp )
QMake .pro
Works
QT += widgets
TARGET = HelloPlugin
TEMPLATE = lib
SOURCES += helloplugin.cpp
HEADERS += helloplugin.h \
helloplugin_global.h
CONFIG += plugin debug
INSTALLS += target