如何使用CMake将静态Qt插件内置到应用程序中?

时间:2017-02-01 10:23:32

标签: qt plugins cmake

我想将自定义虚拟音频设备插件嵌入到应用程序中。 QAudioSystemPlugin看起来适合这项任务。

但它没有加载。

的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"]
}

构建和启动:VirtualQAudioSystemPlugin的构造函数未执行。

如何加载?

更新:我也试图将其设为动态插件,它与QT_DEBUG_PLUGINS=1显示正常,但不会被创建或调用。

更新QT_DEBUG_PLUGINS=1显示静态构建插件时与插件无关的内容。

这是插件是一个单独的库(显示,但仍然无法工作):

(请注意,我还使用&#39;虚拟&#39;替换了virtual.json中的&#39;默认&#39;

QFactoryLoader::QFactoryLoader() checking directory path "/usr/lib/x86_64-linux-gnu/qt5/plugins/audio" ...
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/x86_64-linux-gnu/qt5/plugins/audio/libqtaudio_alsa.so"
Found metadata in lib /usr/lib/x86_64-linux-gnu/qt5/plugins/audio/libqtaudio_alsa.so, metadata=
{
    "IID": "org.qt-project.qt.audiosystemfactory/5.0",
    "MetaData": {
        "Keys": [
            "alsa"
        ]
    },
    "className": "QAlsaPlugin",
    "debug": false,
    "version": 328961
}


Got keys from plugin meta data ("alsa")
QFactoryLoader::QFactoryLoader() looking at "/usr/lib/x86_64-linux-gnu/qt5/plugins/audio/libqtmedia_pulse.so"
Found metadata in lib /usr/lib/x86_64-linux-gnu/qt5/plugins/audio/libqtmedia_pulse.so, metadata=
{
    "IID": "org.qt-project.qt.audiosystemfactory/5.0",
    "MetaData": {
        "Keys": [
            "default"
        ]
    },
    "className": "QPulseAudioPlugin",
    "debug": false,
    "version": 328961
}


Got keys from plugin meta data ("default")
QFactoryLoader::QFactoryLoader() checking directory path "/data/hd2/tmp/plugins/build/audio" ...
QFactoryLoader::QFactoryLoader() looking at "/data/hd2/tmp/plugins/build/audio/libmyproject.so"
Found metadata in lib /data/hd2/tmp/plugins/build/audio/libmyproject.so, metadata=
{
    "IID": "org.qt-project.qt.audiosystemfactory/5.0",
    "MetaData": {
        "Keys": [
            "virtual"
        ]
    },
    "className": "VirtualQAudioSystemPlugin",
    "debug": true,
    "version": 328961
}


Got keys from plugin meta data ("virtual")
loaded library "/usr/lib/x86_64-linux-gnu/qt5/plugins/audio/libqtaudio_alsa.so"
loaded library "/usr/lib/x86_64-linux-gnu/qt5/plugins/audio/libqtmedia_pulse.so"
loaded library "/data/hd2/tmp/plugins/build/audio/libmyproject.so"

2 个答案:

答案 0 :(得分:0)

从我所看到的,它应该是这样的:

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.

您还可以将QT_DEBUG_PLUGINS环境变量设置为1,以获取有关正在发生的事情的更多信息。

答案 1 :(得分:0)

问题是因为 virtual.json 中的'default':它被其他插件覆盖了。如果没有其他插件,则实例化该对象。

不得不换成'虚拟'之类的东西。

{
    "Keys": ["virtual"]
}