Skip to content

Instantly share code, notes, and snippets.

@paulmasri
Created May 16, 2022 13:49
Show Gist options
  • Save paulmasri/f0792302e833c4122e6890cadc6c24d1 to your computer and use it in GitHub Desktop.
Save paulmasri/f0792302e833c4122e6890cadc6c24d1 to your computer and use it in GitHub Desktop.
CMake using qt_add_qml_module: how to handle dependency on QtMultimedia (or other Qt non-core module)
cmake_minimum_required(VERSION 3.16)
project(minimalSynthesizer VERSION 0.1 LANGUAGES CXX)
set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.2 COMPONENTS Quick Multimedia REQUIRED)
qt_add_executable(appSynth
main.cpp
)
qt_add_qml_module(appSynth
URI minimalSynthesizer
VERSION 1.0
QML_FILES main.qml
)
qt_add_qml_module(Synth
URI Synth
VERSION 1.0
SOURCES
Synthesizer.h Synthesizer.cpp
)
target_include_directories(Synthplugin
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
)
#target_link_libraries(Synthplugin
# PRIVATE Qt6::Multimedia
#)
target_compile_definitions(appSynth
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(appSynth
PRIVATE Qt6::Quick Qt6::Multimedia
Synth
)
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(u"qrc:/minimalSynthesizer/main.qml"_qs);
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
import QtQuick
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
}
#include "Synthesizer.h"
Synthesizer::Synthesizer(const QAudioFormat &format, QObject *parent)
: QObject{parent}
, _audioFormat(format)
, _audioSink(nullptr)
{
}
Synthesizer::~Synthesizer()
{
stop();
}
void Synthesizer::startPull(QIODevice *device)
{
stop();
_audioSink = new QAudioSink(_audioFormat);
_audioSink->start(device);
}
void Synthesizer::stop()
{
if (_audioSink == nullptr)
return;
_audioSink->stop();
_audioSink->deleteLater();
_audioSink = nullptr;
}
#pragma once
#include <QObject>
#include <QtMultimedia/QAudioFormat>
#include <QtMultimedia/QAudioSink>
class Synthesizer : public QObject
{
Q_OBJECT
public:
explicit Synthesizer(const QAudioFormat &format, QObject *parent = nullptr);
virtual ~Synthesizer();
void startPull(QIODevice *device);
void stop();
private:
QAudioFormat _audioFormat;
QAudioSink *_audioSink;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment