How to build-in a static Qt plugin into an application with CMake?
Solved
General and Desktop
-
I'd like to embed a custom virtual audio device plugin into an application. The
QAudioSystemPlugin
looks suitable for this task.But it doesn't load.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.11) project(myproject) find_package(Qt5 5.5.0 REQUIRED COMPONENTS Core Multimedia ) set(CMAKE_AUTOMOC ON) add_definitions("-DQT_STATICPLUGIN") add_executable(${PROJECT_NAME} main.cpp VirtualQAudioSystemPlugin.cpp ) target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Multimedia ) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
main.cpp:
#include <iostream> #include <QCoreApplication> #include <QAudioDeviceInfo> #include <QtPlugin> Q_IMPORT_PLUGIN(VirtualQAudioSystemPlugin) int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); for (const auto& dev : QAudioDeviceInfo::availableDevices(QAudio::AudioInput)) std::cerr << dev.deviceName().toStdString() << "\n"; return app.exec(); }
VirtualQAudioSystemPlugin.hpp:
#pragma once #include <QtMultimedia/QAudioSystemPlugin> class VirtualQAudioSystemPlugin: public QAudioSystemPlugin { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.qt.audiosystemfactory/5.0" FILE "virtual.json") public: VirtualQAudioSystemPlugin(QObject *parent = 0); ~VirtualQAudioSystemPlugin() {} virtual QList<QByteArray> availableDevices(QAudio::Mode mode) const override; virtual QAbstractAudioInput* createInput(const QByteArray& device) override; virtual QAbstractAudioOutput* createOutput(const QByteArray& device) override; virtual QAbstractAudioDeviceInfo* createDeviceInfo(const QByteArray& device, QAudio::Mode mode) override; };
VirtualQAudioSystemPlugin.cpp:
#include "VirtualQAudioSystemPlugin.hpp" #include <iostream> VirtualQAudioSystemPlugin::VirtualQAudioSystemPlugin(QObject *parent) : QAudioSystemPlugin(parent) { std::cerr << "VirtualQAudioSystemPlugin\n"; } QList<QByteArray> VirtualQAudioSystemPlugin::availableDevices(QAudio::Mode mode) const { std::cerr << "VirtualQAudioSystemPlugin\n"; } QAbstractAudioInput* VirtualQAudioSystemPlugin::createInput(const QByteArray &device) { std::cerr << "VirtualQAudioSystemPlugin\n"; } QAbstractAudioOutput* VirtualQAudioSystemPlugin::createOutput(const QByteArray &device) { std::cerr << "VirtualQAudioSystemPlugin\n"; } QAbstractAudioDeviceInfo* VirtualQAudioSystemPlugin::createDeviceInfo(const QByteArray &device, QAudio::Mode mode) { std::cerr << "VirtualQAudioSystemPlugin\n"; }
virtual.json:
{ "Keys": ["default"] }
Build and launch: the constructor of the
VirtualQAudioSystemPlugin
is not executed.How to make it load?
-
Hi,
From what I can see, it should rather be something like:
class VirtualQAudioSystemPlugin : public QAudioSystemPlugin, public QAudioSystemPluginExtension { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.qt.audiosystemfactory/5.0" FILE "virtual.json") Q_INTERFACES(QAudioSystemPluginExtension) etc.
You can also set the QT_DEBUG_PLUGINS environment variable to 1 to get more information about what is happening.