Skip to content

Latest commit

 

History

History
118 lines (78 loc) · 2.55 KB

README.md

File metadata and controls

118 lines (78 loc) · 2.55 KB

OSX (Apple Clang - LLVM 3.6), Linux (x64, gcc-5.4, clang-3.8): Build Status

Windows (Win32, x64, msvc2015, MinGW 5.3): Build status

Qt Component Manager

The library helps to build modular applications based on the Qt framework and particularly Qt Meta-Object Sytem.

Qt Component Manager in a nutshell

The Component Manager helps to instantiate QObject derivatives by their identifier without explicitly linking to the library where this class is defined. It is also possible to cast the resulting pointer QObject* to some pre-defined interface (abstract class).

The instantiation is as easy as:

auto someComponent = ComponentManager::create<IComponent>("FancyComponent");

Example

1. Module (Shared Library). Define your objects and register them

// CustomModule.cpp

class CPP_EXPORT_MACRO CustomAction : public QAction
{
  Q_OBJECT // <- important

public:
  Q_INVOKABLE // <- important
  CustomAction(QObject* parent)
    : QAction("Custom Action", parent) {}

  virtual
  ~CustomAction() = default;
};


#include <ComponentManager/ComponentRegistry>

// The only required public function

extern "C"
{
  CPP_EXPORT_MACRO
  void
  registerComponent()
  {
    REGISTER_TYPE(CustomAction);
  }
}

2. Prepare JSON-based module description

CustomModule.json:

{
  "module" : {
    "name" : "CustomModule",

    "path" : "../lib",

    "components" : [
      {
        "name" : "CustomAction",

        "construction" : "prototype",
      }
    ]
  }
}

3. Write main application using modules

#include <ComponentManager/ModuleLoader>
#include <ComponentManager/Creator>

int
main(int argc, char* argv[])
{
  // Create Qt application
  QApplication application(argc, argv);

  // We assume that CustomModule.json is located next to the executable

  QDir directory(QCoreApplication::applicationDirPath());

  QString jsonFile = directory.absoluteFilePath("CustomModule.json");

  // This line loads shared libraries and registers available components

  ComponentManager::loadModule(jsonFile);

  // Create components at any place in your program

  CustomAction* customAction =
    ComponentManager::create<CustomAction*>("CustomAction");

  // Use customAction

  return application.exec();
}