2026-04-02 11:02:19 +02:00
# Tutorial Part 3: Building a C++ UI Module (Process-Isolated)
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
This is Part 3 of the Logos module tutorial series. In [Part 2 ](tutorial-qml-ui-app.md ) you built a QML-only UI plugin. Now you'll build a **ui_qml module with a C++ backend** — the backend runs in a separate `ui-host` process while the QML view loads in the host app (basecamp / standalone).
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
**What you'll build:** A `calc_ui_cpp` module with:
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
- A `.rep` file defining the remote interface (slots + properties)
- A C++ backend plugin that inherits from the generated `SimpleSource` base class
- A QML view that calls the backend via a typed replica using `logos.watch()`
- Process isolation: backend crashes can't bring down the host app
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
**Why C++ backend over QML-only?**
2026-03-19 19:42:15 +01:00
2026-04-21 10:45:21 +01:00
| | QML-only (Part 2) | C++ backend (Part 3) |
| ----------------- | ----------------------------------------------------------------- | --------------------------------------- |
| Compilation | None | CMake + Qt |
| Process isolation | No (QML runs in-process) | Yes (C++ in separate `ui-host` process) |
| Backend calls | `logos.callModule()` / `logos.callModuleAsync()` to other modules | `LogosModules` typed SDK in C++ |
| Type safety | Args travel as `QVariant` | C++ types preserved |
| QML ↔ backend | Direct bridge | Qt Remote Objects (typed replica) |
| `.rep` file | Not needed | Required — defines the remote interface |
2026-03-19 19:42:15 +01:00
**Prerequisites:**
2026-04-17 09:42:47 +02:00
- Completed [Part 1 ](tutorial-wrapping-c-library.md ) — you have a working `calc_module` with the shared library built (`.so` on Linux, `.dylib` on macOS in `logos-calc-module/lib/` )
2026-03-19 19:42:15 +01:00
- Nix with flakes enabled
---
2026-04-02 11:02:19 +02:00
## Architecture
2026-03-19 19:42:15 +01:00
```
2026-04-02 11:02:19 +02:00
logos-basecamp / logos-standalone-app
┌─────────────────────────────────────────────┐
│ │
│ QML View (Main.qml) │
│ readonly property var backend: │
│ logos.module("calc_ui_cpp") │
│ logos.watch(backend.add(1,2))│
│ │ │
│ │ Qt Remote Objects (socket) │
└──────────┼──────────────────────────────────┘
│
ui-host process (separate)
┌──────────┼──────────────────────────────────┐
│ ▼ │
│ CalcUiCppPlugin (backend) │
│ : CalcUiCppSimpleSource │
│ : CalcUiCppViewPluginBase │
│ int add(int a, int b) { │
│ return m_logos->calc_module.add(a,b); │
│ } │
│ │ │
│ │ LogosModules typed SDK │
│ ▼ │
│ calc_module (loaded in ui-host) │
└─────────────────────────────────────────────┘
2026-03-19 19:42:15 +01:00
```
2026-04-02 11:02:19 +02:00
The `.rep` file declares the interface. At build time, Qt's `repc` compiler generates:
2026-04-21 10:45:21 +01:00
2026-04-02 11:02:19 +02:00
- **`CalcUiCppSimpleSource` ** — base class the backend inherits from
- **`CalcUiCppReplica` ** — typed replica the QML view uses
- **`calc_ui_cpp_replica_factory` ** — separate plugin that the host loads to create typed replicas
2026-03-19 19:42:15 +01:00
---
## Step 1: Scaffold
```bash
mkdir logos-calc-ui-cpp && cd logos-calc-ui-cpp
2026-04-02 11:02:19 +02:00
nix flake init -t github:logos-co/logos-module-builder#ui-qml-backend
2026-03-19 19:42:15 +01:00
git init && git add -A
```
2026-04-02 11:02:19 +02:00
This creates the template. We'll customize it for our calculator.
2026-03-19 19:42:15 +01:00
---
2026-04-02 11:02:19 +02:00
## Step 2: metadata.json
2026-03-19 19:42:15 +01:00
```json
{
"name" : "calc_ui_cpp" ,
"version" : "1.0.0" ,
2026-04-02 11:02:19 +02:00
"type" : "ui_qml" ,
2026-03-24 18:36:10 +01:00
"category" : "tools" ,
2026-04-02 11:02:19 +02:00
"description" : "Calculator C++ UI — QML view with process-isolated backend" ,
2026-03-19 19:42:15 +01:00
"main" : "calc_ui_cpp_plugin" ,
2026-04-02 11:02:19 +02:00
"view" : "qml/Main.qml" ,
2026-03-24 18:36:10 +01:00
"icon" : "icons/calc.png" ,
2026-03-19 19:42:15 +01:00
"dependencies" : [ "calc_module" ],
2026-03-24 18:36:10 +01:00
"nix" : {
2026-04-02 11:02:19 +02:00
"packages" : { "build" : [], "runtime" : [] },
2026-03-24 18:36:10 +01:00
"external_libraries" : [],
2026-04-02 11:02:19 +02:00
"cmake" : { "find_packages" : [], "extra_sources" : [] }
2026-03-24 18:36:10 +01:00
}
2026-03-19 19:42:15 +01:00
}
```
2026-04-02 11:02:19 +02:00
Key fields:
2026-04-21 10:45:21 +01:00
2026-04-02 11:02:19 +02:00
- `"type": "ui_qml"` — tells the builder this is a QML view module
- `"main": "calc_ui_cpp_plugin"` — the backend Qt plugin library (without extension)
- `"view": "qml/Main.qml"` — the QML entry point
- `"dependencies": ["calc_module"]` — core modules the backend calls
2026-03-24 18:36:10 +01:00
2026-03-19 19:42:15 +01:00
---
2026-04-02 11:02:19 +02:00
## Step 3: The `.rep` File
Create `src/calc_ui_cpp.rep` :
```rep
class CalcUiCpp
{
PROP(QString status READWRITE)
SLOT(int add(int a, int b))
SLOT(int multiply(int a, int b))
SLOT(int factorial(int n))
SLOT(int fibonacci(int n))
SLOT(QString libVersion())
}
```
This is the **single source of truth** for the remote interface. `repc` generates:
2026-04-21 10:45:21 +01:00
2026-04-02 11:02:19 +02:00
- `rep_calc_ui_cpp_source.h` — `CalcUiCppSimpleSource` with virtual slots the backend overrides
- `rep_calc_ui_cpp_replica.h` — `CalcUiCppReplica` with typed methods and auto-synced properties
**PROP** values auto-sync from backend to QML replica. **SLOT** return values are delivered as `QRemoteObjectPendingReply` — use `logos.watch()` in QML to get them as JS Promises.
---
## Step 4: CMakeLists.txt
2026-03-19 19:42:15 +01:00
```cmake
cmake_minimum_required ( VERSION 3.14 )
project ( CalcUiCppPlugin LANGUAGES CXX )
if ( DEFINED ENV{LOGOS_MODULE_BUILDER_ROOT} )
include ( $ENV{ LOGOS_MODULE_BUILDER_ROOT } /cmake/LogosModule.cmake )
else ()
message ( FATAL_ERROR "LogosModule.cmake not found. Set LOGOS_MODULE_BUILDER_ROOT." )
endif ()
logos_module (
NAME calc_ui_cpp
2026-04-02 11:02:19 +02:00
REP_FILE src/calc_ui_cpp.rep
2026-03-19 19:42:15 +01:00
SOURCES
src/calc_ui_cpp_interface.h
src/calc_ui_cpp_plugin.h
src/calc_ui_cpp_plugin.cpp
)
```
2026-04-02 11:02:19 +02:00
`REP_FILE` tells `logos_module()` to:
2026-04-21 10:45:21 +01:00
2026-04-02 11:02:19 +02:00
1. Run `repc` to generate source/replica headers
2. Generate `LogosViewPluginBase` (typed remoting base class)
3. Build a separate `calc_ui_cpp_replica_factory` shared library
2026-03-19 19:42:15 +01:00
---
2026-04-02 11:02:19 +02:00
## Step 5: C++ Backend Plugin
### `src/calc_ui_cpp_plugin.h`
2026-03-19 19:42:15 +01:00
```cpp
2026-04-01 17:57:24 +02:00
#pragma once
2026-04-02 11:02:19 +02:00
#include <QString>
#include <QVariantList>
#include "calc_ui_cpp_interface.h"
#include "LogosViewPluginBase.h"
#include "rep_calc_ui_cpp_source.h"
2026-04-01 17:57:24 +02:00
class LogosAPI ;
2026-04-02 11:02:19 +02:00
class LogosModules ;
2026-04-01 17:57:24 +02:00
2026-04-02 11:02:19 +02:00
class CalcUiCppPlugin : public CalcUiCppSimpleSource ,
public CalcUiCppInterface ,
public CalcUiCppViewPluginBase
2026-03-25 10:37:15 +01:00
{
Q_OBJECT
2026-04-02 11:02:19 +02:00
Q_PLUGIN_METADATA ( IID CalcUiCppInterface_iid FILE "metadata.json" )
Q_INTERFACES ( CalcUiCppInterface )
2026-03-25 10:37:15 +01:00
public :
explicit CalcUiCppPlugin ( QObject * parent = nullptr );
~ CalcUiCppPlugin () override ;
QString name () const override { return "calc_ui_cpp" ; }
QString version () const override { return "1.0.0" ; }
Q_INVOKABLE void initLogos ( LogosAPI * api );
2026-04-02 11:02:19 +02:00
// Slots from .rep — override the generated virtuals
int add ( int a , int b ) override ;
int multiply ( int a , int b ) override ;
int factorial ( int n ) override ;
int fibonacci ( int n ) override ;
QString libVersion () override ;
2026-03-25 10:37:15 +01:00
signals :
void eventResponse ( const QString & eventName , const QVariantList & args );
private :
LogosAPI * m_logosAPI = nullptr ;
2026-04-02 11:02:19 +02:00
LogosModules * m_logos = nullptr ;
2026-03-25 10:37:15 +01:00
};
```
2026-04-02 11:02:19 +02:00
Three base classes:
2026-04-21 10:45:21 +01:00
2026-04-02 11:02:19 +02:00
- **`CalcUiCppSimpleSource` ** — generated from `.rep` , provides the typed source for Qt Remote Objects
- **`CalcUiCppInterface` ** — standard Logos plugin interface (`name()` , `version()` )
- **`CalcUiCppViewPluginBase` ** — generated, provides `setBackend()` and `enableRemoting()`
2026-03-25 10:37:15 +01:00
2026-04-02 11:02:19 +02:00
### `src/calc_ui_cpp_plugin.cpp`
2026-03-19 19:42:15 +01:00
```cpp
2026-04-02 11:02:19 +02:00
#include "calc_ui_cpp_plugin.h"
#include "logos_api.h"
#include "logos_sdk.h"
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
CalcUiCppPlugin :: CalcUiCppPlugin ( QObject * parent )
: CalcUiCppSimpleSource ( parent ) {}
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
CalcUiCppPlugin ::~ CalcUiCppPlugin () { delete m_logos ; }
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
void CalcUiCppPlugin :: initLogos ( LogosAPI * api )
2026-03-19 19:42:15 +01:00
{
2026-04-02 11:02:19 +02:00
m_logosAPI = api ;
m_logos = new LogosModules ( api );
// Register this object as the Remote Objects source
setBackend ( this );
}
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
int CalcUiCppPlugin :: add ( int a , int b )
{
return m_logos -> calc_module . add ( a , b );
}
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
int CalcUiCppPlugin :: multiply ( int a , int b )
{
return m_logos -> calc_module . multiply ( a , b );
}
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
int CalcUiCppPlugin :: factorial ( int n )
{
return m_logos -> calc_module . factorial ( n );
}
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
int CalcUiCppPlugin :: fibonacci ( int n )
{
return m_logos -> calc_module . fibonacci ( n );
}
QString CalcUiCppPlugin :: libVersion ()
{
return m_logos -> calc_module . libVersion ();
}
2026-03-19 19:42:15 +01:00
```
2026-04-02 11:02:19 +02:00
Key points:
2026-04-21 10:45:21 +01:00
2026-04-02 11:02:19 +02:00
- Constructor calls `CalcUiCppSimpleSource(parent)` — not `QObject(parent)`
- `initLogos()` calls `setBackend(this)` to register with the Remote Objects host
- Slots return values directly — they travel back to the QML replica via Qt Remote Objects
- `m_logos->calc_module.add(a, b)` uses the generated typed SDK (type-safe, no QVariant)
2026-03-19 19:42:15 +01:00
---
2026-04-02 11:02:19 +02:00
## Step 6: QML View
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
Create `src/qml/Main.qml` :
2026-03-19 19:42:15 +01:00
```qml
import QtQuick
import QtQuick . Controls
import QtQuick . Layouts
2026-04-02 11:02:19 +02:00
2026-03-19 19:42:15 +01:00
Item {
id: root
property string result: ""
property string errorText: ""
2026-04-02 11:02:19 +02:00
// Typed replica of the backend running in ui-host
readonly property var backend: logos . module ( "calc_ui_cpp" )
// "status" property from the .rep — auto-synced via Qt Remote Objects
readonly property string status: backend ? backend.status : ""
function callCalc ( method , args ) {
if ( ! backend ) {
root . errorText = "Backend not available"
return
}
root . errorText = ""
root . result = "..."
// logos.watch() wraps the pending reply in a JS Promise
2026-04-21 10:46:34 +01:00
logos . watch ( backend [ method ]. apply ( backend , args ),
2026-04-02 11:02:19 +02:00
function ( value ) { root . result = String ( value ) },
function ( error ) { root . errorText = String ( error ) }
)
}
2026-03-19 19:42:15 +01:00
ColumnLayout {
anchors.fill: parent
anchors.margins: 24
spacing: 16
Text {
2026-04-02 11:02:19 +02:00
text: "Calculator (C++ backend)"
2026-03-19 19:42:15 +01:00
font.pixelSize: 20
color: "#ffffff"
}
RowLayout {
spacing: 12
2026-04-02 11:02:19 +02:00
TextField {
id: inputA ; placeholderText: "a"
Layout.preferredWidth: 80
validator: IntValidator {}
}
TextField {
id: inputB ; placeholderText: "b"
Layout.preferredWidth: 80
validator: IntValidator {}
}
2026-03-19 19:42:15 +01:00
Button {
text: "Add"
2026-04-02 11:02:19 +02:00
onClicked: root . callCalc ( "add" , [ parseInt ( inputA . text ) || 0 ,
parseInt ( inputB . text ) || 0 ])
2026-03-19 19:42:15 +01:00
}
Button {
text: "Multiply"
2026-04-02 11:02:19 +02:00
onClicked: root . callCalc ( "multiply" , [ parseInt ( inputA . text ) || 0 ,
parseInt ( inputB . text ) || 0 ])
2026-03-19 19:42:15 +01:00
}
}
Rectangle {
2026-04-02 11:02:19 +02:00
Layout.fillWidth: true ; height: 56
color: root . errorText ? "#3d1a1a" : "#1a2d1a"
2026-03-19 19:42:15 +01:00
radius: 8
Text {
anchors.centerIn: parent
2026-04-02 11:02:19 +02:00
text: root . errorText || root . result || "Press a button"
color: root . errorText ? "#f85149" : "#56d364"
2026-03-19 19:42:15 +01:00
font.pixelSize: 15
}
}
2026-04-02 11:02:19 +02:00
Text {
text: "Backend status: " + root . status
color: "#8b949e" ; font.pixelSize: 13
}
2026-03-19 19:42:15 +01:00
}
}
```
2026-04-02 11:02:19 +02:00
Key patterns:
2026-04-21 10:45:21 +01:00
2026-04-02 11:02:19 +02:00
- `logos.module("calc_ui_cpp")` — gets the typed replica (auto-synced properties)
- `backend.status` — PROP from `.rep` , updates automatically
2026-04-21 10:46:34 +01:00
- `logos.watch(backend.add(1, 2), ...)` — SLOT return value as JS Promise
2026-04-21 10:45:21 +01:00
- ``— required for` logos.watch()`
2026-03-19 19:42:15 +01:00
---
2026-04-02 11:02:19 +02:00
## Step 7: flake.nix
2026-03-19 19:42:15 +01:00
```nix
{
2026-04-02 11:02:19 +02:00
description = "Calculator C++ UI plugin — QML view with process-isolated backend" ;
2026-03-19 19:42:15 +01:00
inputs = {
2026-04-02 11:02:19 +02:00
logos-module-builder . url = "github:logos-co/logos-module-builder" ;
2026-04-17 09:42:47 +02:00
# Option A: point to a remote repo (for CI or when calc_module is published)
2026-04-02 11:02:19 +02:00
calc_module . url = "github:logos-co/logos-tutorial?dir=logos-calc-module" ;
2026-04-17 09:42:47 +02:00
# Option B: point to your local checkout (for local development)
# calc_module.url = "path:../logos-calc-module";
2026-03-19 19:42:15 +01:00
};
2026-04-02 11:02:19 +02:00
outputs = inputs @ { logos-module-builder , ... }:
logos-module-builder . lib . mkLogosQmlModule {
2026-03-23 21:59:59 +01:00
src = ./. ;
2026-03-24 18:36:10 +01:00
configFile = ./metadata.json ;
flakeInputs = inputs ;
2026-03-23 21:59:59 +01:00
};
2026-03-19 19:42:15 +01:00
}
```
2026-04-17 09:42:47 +02:00
The `calc_module` input attribute name must match the dependency name in `metadata.json` . The URL can be:
2026-04-21 10:45:21 +01:00
2026-04-17 09:42:47 +02:00
- **`github:` ** — fetches from a remote GitHub repo. Use for CI or when `calc_module` is published.
- **`path:` ** — points to a local directory on disk (e.g., `path:../logos-calc-module` ). Use during local development.
> **Important:** Whichever URL scheme you use, `calc_module` must be built with its shared library (`.so` on Linux, `.dylib` on macOS) present in `lib/`. If it's missing, the nix build will fail with linker errors. See [Part 1, Step 1.5](tutorial-wrapping-c-library.md#15-build-the-shared-library).
2026-04-02 11:02:19 +02:00
`mkLogosQmlModule` handles everything: compiles the C++ backend (because `main` is set), bundles the QML view, generates LGX packages, and wires up `nix run` .
2026-03-23 21:59:59 +01:00
2026-03-19 19:42:15 +01:00
---
2026-04-02 11:02:19 +02:00
## Step 8: Build and Run
2026-03-19 19:42:15 +01:00
2026-04-17 09:42:47 +02:00
First, make sure your local `calc_module` is built and its `.so` /`.dylib` is present in `lib/` (see [Part 1, Step 1.5 ](tutorial-wrapping-c-library.md#15-build-the-shared-library )):
```bash
ls ../logos-calc-module/lib/libcalc.so # Linux
ls ../logos-calc-module/lib/libcalc.dylib # macOS
```
Then build and run. Choose the approach that matches your `flake.nix` setup:
2026-03-19 19:42:15 +01:00
```bash
git add -A
2026-04-17 09:42:47 +02:00
# If flake.nix uses path:../logos-calc-module — just run directly:
nix run
# If flake.nix uses github: — override to use your local checkout:
nix run --override-input calc_module path:../logos-calc-module
2026-04-02 11:02:19 +02:00
# Or from the workspace:
./scripts/ws run logos-calc-ui-cpp --local logos-calc-ui-cpp logos-calc-module
2026-03-19 19:42:15 +01:00
```
---
2026-04-02 11:02:19 +02:00
## Step 9: How the Pieces Connect
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
1. `nix build` → compiles the C++ plugin + replica factory, bundles QML view
2. `nix run` → launches `logos-standalone-app` which:
- Loads `calc_module` (dependency)
- Spawns a `ui-host` child process with `calc_ui_cpp_plugin.so`
- `ui-host` calls `initLogos()` → `setBackend(this)` → `enableRemoting(host)`
- Backend is now accessible over a local socket
3. Host app loads `calc_ui_cpp_replica_factory.dylib` → creates a typed replica
4. QML gets the replica via `logos.module("calc_ui_cpp")`
5. `backend.add(1, 2)` → Qt Remote Objects sends call to ui-host → backend runs → returns result
6. `backend.status` auto-syncs whenever the backend calls `setStatus(...)`
2026-03-19 19:42:15 +01:00
2026-03-25 10:37:15 +01:00
---
2026-04-14 14:04:57 +02:00
## Step 10: UI Integration Tests (Optional)
Add automated UI tests using the [logos-qt-mcp ](https://github.com/logos-co/logos-qt-mcp ) test framework. Just create `.mjs` files in `tests/` and `logos-module-builder` auto-wires `nix build .#integration-test` .
Create `tests/ui-tests.mjs` :
```javascript
import { resolve } from "node:path" ;
// CI sets LOGOS_QT_MCP automatically; for interactive use: nix build .#test-framework -o result-mcp
2026-04-21 10:45:21 +01:00
const root =
process . env . LOGOS_QT_MCP ||
new URL ( "../result-mcp" , import . meta . url ). pathname ;
const { test , run } = await import (
resolve ( root , "test-framework/framework.mjs" )
);
2026-04-14 14:04:57 +02:00
test ( "calc_ui_cpp: loads and shows title" , async ( app ) => {
await app . waitFor (
2026-04-21 10:45:21 +01:00
async () => {
await app . expectTexts ([ "UI Example (C++ backend)" ]);
},
{ timeout : 15000 , interval : 500 , description : "UI to load" },
2026-04-14 14:04:57 +02:00
);
});
test ( "calc_ui_cpp: shows connection status" , async ( app ) => {
await app . expectTexts ([ "Connecting to backend..." ]);
});
test ( "calc_ui_cpp: add button visible" , async ( app ) => {
await app . expectTexts ([ "Add" ]);
});
run ();
```
```bash
git add tests/
# Hermetic CI test
nix build .#integration-test -L
# Interactive
nix build .#test-framework -o result-mcp
nix run . # app with inspector on :3768
node tests/ui-tests.mjs # in another terminal
```
---
2026-04-02 11:02:19 +02:00
## Comparison: .rep Interface Patterns
2026-03-25 10:37:15 +01:00
2026-04-21 10:45:21 +01:00
| Pattern | .rep declaration | Backend C++ | QML usage |
| ---------------- | ------------------------------------ | ------------------------------------------- | ---------------------------------------------------------------------- |
2026-04-21 10:46:34 +01:00
| **Return value** | `SLOT(int add(int a, int b))` | `int add(...) override { return ...; }` | `logos.watch(backend.add(1,2), cb)` |
2026-04-21 10:45:21 +01:00
| **Property** | `PROP(QString status READWRITE)` | `setStatus("Ready")` (inherited) | `backend.status` (auto-syncs) |
| **Signal** | `SIGNAL(errorOccurred(QString msg))` | `emit errorOccurred("fail")` | `Connections { target: backend; function onErrorOccurred(msg) {...} }` |
| **Model** | (use Q_PROPERTY on backend) | `Q_PROPERTY(QAbstractItemModel* items ...)` | `logos.model("calc_ui_cpp", "items")` |
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
---
2026-03-19 19:42:15 +01:00
2026-04-02 11:02:19 +02:00
## Next Steps
- Add more `.rep` properties/signals for richer UI state
- Use `logos.model()` for list views backed by `QAbstractItemModel`
- Package as `.lgx` for distribution: `nix build .#lgx`
- See [logos-package-manager-ui ](https://github.com/logos-co/logos-package-manager-ui ) for a production example