Qt World Summit: Submit your Presentation

Qt Pluggin issue

  • Hello,
    I have a question regarding Qt Pluggin.
    I am doing a little audio application which plays sounds (and other stuff) from instruments.
    I have an abstract class Instrument and the instruments subclass this Instrument class for exemple Piano, etc ...
    I want to make some of these instruments available as plugins. For example I created Violon.dll as Qt Plugin when I do a new Violin in my code its working fine.
    However, if after the release of the binary, I want to create another instrument lets say Cello, I won't be able to write the piece of code with Instrument* instrument = new Cello.
    How could I make that working if I don't know before the release, the instruments I will create after the release of the binary?

  • For example, here is a piece of code I found...

    @QObject *plugin = pluginLoader.instance();
    if (plugin) {
    myInstrument = qobject_cast<Instrument *>(plugin);@

    The only issue I have is that I have a menu in the GUI which is showing all the instruments available. I can dynamically add the new instrument names to this list using myInstrument->getName() but can I say if you click on "Cello" in the menu it creates a Cello, etc ... Cause to do myInstrument->getName() you already have an instance of an implemented Instrument... It's messy for me :(

  • Can this work :
    You create an instance of all the instrument pluggins to have access to their name and load the names in the GUI menu.
    @foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
    QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
    QObject *plugin = pluginLoader.instance();
    if (plugin) {
    myInstrument = qobject_cast<Instrument *>(plugin);
    if (myInstrument)
    return true;

    Then call createInstrument(QString name):

    @void createInstrument(QString name){
    // assuming the instrumentName is the same as the dll file name
    QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(name));
    QObject *plugin = pluginLoader.instance();
    if (plugin) {
    myInstrument = qobject_cast<Instrument *>(plugin);


    Should this work?

  • Lifetime Qt Champion


    Did you take a look at the "Plug and Paint" example ? It seems to fit your use case

    Hope it helps

  • Hello SGaist,
    Didn't have a look at that particular exemple.
    Thank you very much,

Log in to reply