Walkthrough: porting a widgets window to QML while it runs

This walkthrough has an agent write a Qt Quick version of a Qt Widgets window inside the running application. The QML binds to the application’s real objects. The agent compares the two windows, reloads the QML until they match, and saves the result so it can be opened again later.

What it demonstrates:

Who it’s for: anyone weighing a move from Qt Widgets to Qt Quick, or curious what the QML window tools can do beyond debugging. It reads best after the packet-lag walkthrough, which introduces ui_open. You need the project built and about 20 minutes.

All outputs are real, trimmed to the relevant lines. They come from more than one run, so object ids don’t always line up between steps; yours will differ too.

The request

“We’re thinking about moving Parts Bin to Qt Quick. Write a QML version of its main window, bound to the real inventory, so we can see how far the current backend gets us. Keep it once it works.”

Parts Bin (examples/parts-bin) is a small Qt Widgets inventory: a filterable table of parts, buttons for the selected part, a form for a new one, and the totals.

Set up

The example has a .sonde/config.json, so sonde builds and starts it:

cd examples/parts-bin && ../../build/sonde start && cd ../..
export SONDE_URL=http://127.0.0.1:8799/mcp
mcp() { build/sonde "$@"; }
built in 0.0 s
started pid 46892
ready: local socket parts-bin-mcp, http://127.0.0.1:8799/mcp

The repository already contains the finished port, in the example’s own template library, examples/parts-bin/.sonde/qml/. sonde starts the application in examples/parts-bin, so that is the project library it finds. To follow along from scratch, move the port out of the way first:

mv examples/parts-bin/.sonde/qml /tmp/parts-bin-qml

Parts Bin calls installQmlTools(), like a Qt Quick application would. A Qt Widgets application has no QML engine of its own, so it links Sonde::Quick for ui_open; that is the only change a port like this needs in the application.

Step 1: read the original

mcp window_screenshot

Parts Bin: a table of ten parts, three of them red

Three rows are red, and the status bar names the same three parts. The widget tree shows the window’s structure and the names to aim at:

mcp object_tree '{"root": "binWindow", "className": "QWidget"}'
@0x16b535bf8 QMainWindow#binWindow windowTitle="Parts Bin"
  @0x808f169d0 QWidget
    @0x808f16850 QLineEdit#filterEdit placeholderText="Filter by SKU or name"
    @0x808c4c930 QTableView#partsTable
    @0x8093a2dc0 QPushButton#takeOne text="Take one" (disabled)
    @0x808f166d0 QPushButton#restock text="Restock +10" (disabled)
    @0x808fa60a0 QPushButton#removePart text="Remove" (disabled)
    @0x808fa6340 QGroupBox#newPartBox title="New part"
      ...
      @0x8093a2e80 QSpinBox#quantitySpin text="0"
      @0x808f165b0 QDoubleSpinBox#priceSpin text="$0.00"
      @0x8093a2fd0 QPushButton#addPart text="Add part"
    @0x8093c16b0 QLabel#totalsLabel text="10 parts, 649 pieces, worth $34.13"
  @0x808fa6370 QStatusBar

But widgets are what a port replaces. What matters is what the QML can bind to. One level under the application are two objects that aren’t widgets:

mcp object_tree '{"depth": 1}'
@0x16b535cf0 QApplication
  @0x109001da0 QFusionStyle#fusion
  @0x808c4c6f0 Inventory#inventory
  @0x10901b7c0 QSortFilterProxyModel#inventoryFilter

QFusionStyle says the widgets are drawn in Fusion, so the QML should import QtQuick.Controls.Fusion to look the same. And inventory is the data:

mcp object_inspect '{"object": "inventory"}'
@0x808c4c6f0 Inventory#inventory
inherits: Inventory > QAbstractTableModel > QAbstractItemModel > QObject

properties:
  count: int = 10 (read-only)
  totalQuantity: int = 649 (read-only)
  totalValue: double = 34.129999999999995 (read-only)

signals:
  void countChanged()
  void totalsChanged()
  ...
invokables:
  ...
  bool addPart(QString,QString,int,double)
  bool adjust(QString,int)
  bool removePart(QString)

The totals have NOTIFY signals, so QML bindings to them update. There is a method behind each button. model_read gives the roles a QML delegate can use:

mcp model_read '{"object": "inventoryFilter", "rows": 1}'
@0x10901b7c0 QSortFilterProxyModel#inventoryFilter: 10 row(s) x 5 column(s)
columns: SKU | Part | Qty | Unit price | Value
0: display=R-10K name=Resistor 10 kΩ price=0.02 quantity=240 sku=R-10K value=4.8 | ...

So it has sku, name, quantity, price and value. That’s enough for a first draft.

Step 2: a first draft

The agent writes the window: a ListView over the filter model, the buttons calling inventory.adjust() and inventory.removePart(), the form calling inventory.addPart(), and a label bound to the totals. In a file, draft.qml:

draft.qml (75 lines) ```qml import QtQuick import QtQuick.Controls.Fusion import QtQuick.Layouts ApplicationWindow { width: 560; height: 520 visible: true ColumnLayout { anchors.fill: parent anchors.margins: 9 TextField { Layout.fillWidth: true placeholderText: "Filter by SKU or name" onTextChanged: parts.setFilterFixedString(text) } ListView { id: list Layout.fillWidth: true Layout.fillHeight: true clip: true model: parts currentIndex: -1 delegate: ItemDelegate { required property int index required property string sku required property string name required property int quantity required property real price required property real value width: ListView.view.width highlighted: ListView.isCurrentItem onClicked: list.currentIndex = index contentItem: RowLayout { Label { text: sku; Layout.preferredWidth: 80 } Label { text: name; Layout.fillWidth: true } Label { text: quantity; Layout.preferredWidth: 60; horizontalAlignment: Text.AlignRight } Label { text: "$" + price.toFixed(2); Layout.preferredWidth: 80; horizontalAlignment: Text.AlignRight } Label { text: "$" + value.toFixed(2); Layout.preferredWidth: 80; horizontalAlignment: Text.AlignRight } } } } RowLayout { readonly property string sku: list.currentItem ? list.currentItem.sku : "" id: actions Button { text: "Take one"; enabled: actions.sku !== ""; onClicked: inventory.adjust(actions.sku, -1) } Button { text: "Restock +10"; enabled: actions.sku !== ""; onClicked: inventory.adjust(actions.sku, 10) } Item { Layout.fillWidth: true } Button { text: "Remove"; enabled: actions.sku !== ""; onClicked: inventory.removePart(actions.sku) } } GroupBox { title: "New part" Layout.fillWidth: true GridLayout { anchors.fill: parent columns: 2 Label { text: "SKU:" } TextField { id: sku; Layout.fillWidth: true } Label { text: "Name:" } TextField { id: name; Layout.fillWidth: true } Label { text: "Quantity:" } RowLayout { SpinBox { id: quantity; to: 100000; editable: true } Label { text: "at" } SpinBox { id: price; to: 10000000; editable: true } Item { Layout.fillWidth: true } Button { text: "Add part" onClicked: { if (inventory.addPart(sku.text, name.text, quantity.value, price.value / 100)) { sku.clear(); name.clear() } } } } } } Label { text: inventory.count + " parts, " + inventory.totalQuantity + " pieces, worth $" + inventory.totalValue.toFixed(2) } } } ```

Then it opens the window, with the two objects passed in by name. acceptFocus is there because the window has text fields:

qml() { python3 -c 'import json, sys; print(json.dumps({"qml": open(sys.argv[1]).read(), "objects": {"inventory": "inventory", "parts": "inventoryFilter"}, "title": "Parts Bin (QML)", "acceptFocus": True} | json.loads(sys.argv[2])))' "$@"; }
mcp ui_open "$(qml draft.qml '{}')"
Opened @0xb1e224e00 ApplicationWindow_QMLTYPE_30 "Parts Bin (QML)" (560x520). Close it with ui_close {"window": "@0xb1e224e00"}; window_screenshot and object_tree work on it too.
Objects: inventory = @0xb1f0402d0 Inventory#inventory; parts = @0x1078d73f0 QSortFilterProxyModel#inventoryFilter
mcp window_screenshot '{"window": "Parts Bin (QML)"}'

The first draft: the same rows, no header, no red rows, no status bar

Close, from the first try. Next to the original, four things differ:

  1. There is no header row and there are no lines between rows.
  2. No row is red.
  3. There is no status bar naming the parts that are low on stock.
  4. The price box shows 0, not $0.00. A SpinBox counts in integers, so the draft counts cents, and it needs to show them as dollars.

Step 3: reload until it matches

1, 3 and 4 are plain QML: a header for the ListView, a footer for the window, textFromValue for the price box. The agent edits the file and opens it again, with replace naming the window to swap out:

mcp ui_open "$(qml draft.qml '{"replace": "Parts Bin (QML)"}')"
Opened @0xb1e2fe200 ApplicationWindow_QMLTYPE_30_QML_36 "Parts Bin (QML)" (560x520). ...

If the new QML doesn’t compile, the error comes back with line and column, and the old window stays open. Each try takes a second, so the loop is: edit, ui_open with replace, screenshot, compare.

The windows look alike now. The next question is whether they work alike. The agent selects C-10U in the QML window and clicks its Restock +10:

mcp input_click '{"window": "Parts Bin (QML)", "x": 200, "y": 157}'
mcp input_click '{"window": "Parts Bin (QML)", "object": "Button[text=Restock +10]"}'
mcp model_read '{"object": "partsTable", "find": "C-10U", "roles": ["display"]}'
3: C-10U | Capacitor 10 µF | 13 | $0.12 | $1.56

The widgets table shows the change too: 13 pieces, and the row is no longer red. The totals label and the status bar caught up as well. Both windows show one inventory.

Step 4: what the backend doesn’t give the port

Two things came up that QML alone can’t fix. They are the part of this walkthrough worth taking to a real port.

The filter belongs to the widgets table

The draft filters by calling setFilterFixedString() on inventoryFilter. Type in the QML window’s filter field (window matters here: Sonde never sends input to an agent’s window by default):

mcp input_text '{"window": "Parts Bin (QML)", "text": "led"}'
mcp model_read '{"object": "partsTable", "roles": ["display"]}'
mcp object_inspect '{"object": "filterEdit"}'
@0x108a31c20 QSortFilterProxyModel#inventoryFilter: 2 row(s) x 5 column(s), shown by @0x955044990 QTableView#partsTable
0: LED-RED | LED, red 5 mm | 60 | $0.08 | $4.80
1: LED-GRN | LED, green 5 mm | 4 | $0.08 | $0.32
  text: QString = ""

The widgets table now shows only the two LEDs, under an empty filter field. The proxy model isn’t backend state; it’s the table’s view of it. A second front end needs a filter of its own. So the port takes inventory and filters in its delegate, and no longer gets inventoryFilter at all.

The low-stock rule is in the widgets

Red rows need a rule: which parts are low on stock? The model doesn’t say. It has no background role, and no property or role that says “low”:

mcp model_read '{"object": "partsTable", "roles": ["display", "background"], "rows": 1}'
Unknown role 'background'. Roles: display, edit, toolTip, statusTip, whatsThis, checkState, decoration, textAlignment, value, quantity, sku, name, price

So the red is painted by the view: object_tree on partsTable lists a QStyledItemDelegate of its own, and the rule is C++ inside it. The screenshot shows rows with 2, 3 and 4 pieces as red, and 12 as not. To find the exact threshold, the agent tests the running process. It watches the status bar and moves IC-555 (12 pieces) across the line:

mcp signal_watch '{"object": "QStatusBar", "signal": "messageChanged"}'
mcp object_invoke '{"object": "inventory", "method": "adjust", "args": ["IC-555", -7]}'
mcp object_invoke '{"object": "inventory", "method": "adjust", "args": ["IC-555", -1]}'
mcp object_invoke '{"object": "inventory", "method": "adjust", "args": ["IC-555", 1]}'
mcp event_read '{"sources": ["signals"]}'
2 event(s); lastSequence=4; buffer holds #1..#4
#3 info signals[messageChanged]: @0xb4f319440 QStatusBar messageChanged("Low on stock: LED-GRN, IC-555, IC-7805")
#4 info signals[messageChanged]: @0xb4f319440 QStatusBar messageChanged("Low on stock: LED-GRN, IC-7805")

Down to 5 pieces, the message didn’t change, so there is no event for it. At 4, IC-555 joins the list (#3), and back at 5 it leaves again (#4). (C-10U isn’t on the list any more: Step 3 restocked it.) So at 5 pieces a part is fine, and at 4 it is low: the rule is “fewer than 5”. The port copies the number, with a comment saying where it came from.

For a real port that is the finding to report. The rule is business logic, it lives in a widget, and a second front end has to duplicate it. The fix is in the backend, where both front ends can use it. In Inventory:

Q_PROPERTY(int reorderLevel READ reorderLevel CONSTANT)
// and a role, so a QML delegate can write `color: low ? ... : ...`
case LowRole:
    return p.quantity < reorderLevel();

The repository keeps the rule in the widgets, so you can try this yourself.

Step 5: keep it

The header row and the delegate both lay out the same five columns, so the agent makes the columns a component that every template in the library can use. Then it saves the window as a template, with a header that says what it needs:

mcp ui_template_save '{"name": "PartColumns", "component": true, "library": "project", "qml": "..."}'
mcp ui_template_save '{"name": "parts-bin", "library": "project", "qml": "// sonde-template\n// description: ...\n..."}'
Saved PartColumns to /Users/you/sonde/examples/parts-bin/.sonde/qml/components/PartColumns.qml. It is trusted in this checkout only. Templates in the same library use it after `import "components"`.
Saved parts-bin to /Users/you/sonde/examples/parts-bin/.sonde/qml/parts-bin.qml. It is trusted in this checkout only. Open it with ui_open {"template": "parts-bin"}.

The template’s header names its object, so opening it needs nothing else:

// sonde-template
// description: Parts Bin's main window, ported to Qt Quick and bound to the live inventory.
// object: inventory = inventory
// written-for: parts-bin
import QtQuick
import QtQuick.Controls.Fusion
import QtQuick.Layouts
import "components"
mcp ui_open '{"template": "parts-bin", "acceptFocus": true}'
Opened @0x7f7112e00 ApplicationWindow_QMLTYPE_65_QML_74 "parts-bin" (560x520). ...
Objects: inventory = @0x7f7054510 Inventory#inventory
Template: parts-bin (project library, /Users/you/sonde/examples/parts-bin/.sonde/qml/parts-bin.qml)

Using it turned up one more bug. The agent added a part through the QML form, a diode at 0.04 dollars, and read it back from the model:

mcp model_read '{"object": "inventory", "row": 10, "roles": ["display"]}'
10: D-1N4148 | Signal diode 1N4148 | 50 | $4.00 | $200.00

Four dollars, not four cents. A SpinBox’s default validator only accepts digits, so the “.” was dropped and “0.04” became “004”. A RegularExpressionValidator fixes it. The agent saves the template again with replace: true, and reloads the open window by its title:

mcp ui_open '{"template": "parts-bin", "acceptFocus": true, "replace": "parts-bin"}'

The same steps then give $0.04 | $2.00. Checking the result in the model, not on the screen, is what caught this: the screen showed exactly what had been typed.

Step 6: open it in another checkout

The template is committed with the code, in .sonde/qml/ next to the application’s .sonde/config.json. In another clone, or for another developer, the agent’s save doesn’t count. Trust is kept per user and per checkout, and these files are new there:

mcp ui_open '{"template": "parts-bin"}'
Template 'parts-bin' can't open: these files aren't trusted here (design 0023):
- /Users/you/sonde/examples/parts-bin/.sonde/qml/parts-bin.qml: new
Trusting them is the user's decision: ask them to read the files, then to run
  sonde trust parts-bin
Then open it again. Files it imports that aren't trusted either are named then.

Read parts-bin.qml and PartColumns.qml, then trust the library. sonde trust finds it the way the application does, from the directory it runs in:

cd examples/parts-bin && ../../build/sonde trust --all && cd ../..
trusted /Users/you/sonde/examples/parts-bin/.sonde/qml/parts-bin.qml (project library, sha256 7c75495adb99)
trusted /Users/you/sonde/examples/parts-bin/.sonde/qml/components/PartColumns.qml (project library, sha256 c97c9c2ea8bb)
mcp ui_open '{"template": "parts-bin", "acceptFocus": true}'
mcp window_screenshot '{"window": "parts-bin"}'

The finished port: header, red rows and a status bar, like the original

Doing this with an AI agent

Add the application to Claude Code (claude mcp add --transport http parts-bin http://127.0.0.1:8799/mcp, or sonde bridge in .mcp.json; see agent-loop.md), then give it the request from The request. The steps above are the ones an agent takes. It’s worth adding two things to the prompt:

The second one is what turns a mock-up into an answer to “how far does the current backend get us?”.

What made the difference

Applying this to your own application

Cleaning up

cd examples/parts-bin && ../../build/sonde stop && cd ../..

If you moved the finished port out of the way in Set up, put it back, or git checkout examples/parts-bin/.sonde/qml.