agentleFS
Facta-DevFacta-Dev/beautifulqt/skills/qt-best-practices/SKILL.md

qt-best-practices

Skill0 starsChanged 19 days agoMITView on GitHub

Write idiomatic, fast, maintainable Qt 6 desktop applications in C++20. Use this skill whenever you write, review, refactor, debug, or explain a Qt app: Widgets or Qt Quick/QML screens, QObject and signals/slots, Q_PROPERTY and bindable properties, QAbstractItemModel, QFuture/QtConcurrent, the SDK and QNetworkAccessManager, SQLite, CMake targets, platform #ifdefs, packaging or updates. Trigger on any mention of Qt, QML, Widgets, QObject, signals/slots, Q_PROPERTY, QAbstractItemModel, QtConcurrent, QNetworkAccessManager, CMake with Qt, or "fix this window" even when the user never says "best practices". Synthesized from the Qt 6 documentation, the Qt Widgets/Quick guidance, and the facta desktop handbook.

The file

We split this skill into the rules it gives an agent

10 rules. About 0 are common, 5 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Budget first, features second: Every release is measured against a low-end reference machine.

    line 32
  2. Distinctive:

    Run these and fix everything they report:

    line 51
  3. Distinctive:

    Test the release build on the reference floor machine, not only a dev build on a workstation.

    line 66
  4. Very specific:

    No blocking call on the GUI thread: No synchronous network, no large file read, no QProcess::waitForFinished, no QThread::sleep, no heavy …

    line 71
  5. Very specific:

    Exceptions never cross a slot boundary: Use Result<T>/std::expected for expected failures.

    line 83
  6. Distinctive:

    The GUI thread never blocks: No synchronous network, disk, parse or crypto on it.

    line 104
  7. Distinctive:

    When a change would cross a layer, re-cut the layout: move the logic to the layer that owns it.

    line 115
  8. Very specific:

    A QObject child dies with its parent: Do not delete a child that has a parent.

    line 156
  9. Very specific:

    The local machine is untrusted: Verify update signatures, load plugins only from trusted paths, and never eval remote content.

    line 199
  10. Very specific:

    Layouts are cheap: avoid deep nesting, repeated sizeHint work, and per-paint allocations in paintEvent.

    line 219

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Qt best practices

Playbook set: start with PLAYBOOK_MASTER.md — the catalog, precedence and cross-playbook invariants — then PLAYBOOK_DESKTOP_QT.md. This skill is the runnable companion of that contract.

Distilled from the Qt 6 documentation, the Widgets/Quick guidance, and the facta desktop handbook (PLAYBOOK_DESKTOP_QT.md). The digests live in references/; where they disagree, this file says which to follow and why.

1. Mindset

  • Keep the GUI thread sacred. Nothing that can block (network, disk, parsing, crypto, big loops) runs on it. Responsiveness on a ten-year-old dual-core is the acceptance test.
  • Budget first, features second. Every release is measured against a low-end reference machine; a regression fails CI.
  • No GPU assumption. The UI works with software rendering. A GPU path is an optimization with a tested fallback.
  • Domain and application know nothing about widgets, QML, sockets or the OS. They talk through ports.
  • The backend is the source of truth. The desktop holds a cache (projection) and an outbox; it never invents authority.
  • Realtime is optional and degradable. The app is fully correct with polling or no streaming; SSE/WebSocket only make it fresher.
  • Slices mirror the backend. Same names, same permissions, same contracts.
  • The local machine is untrusted. Secrets live in the OS keystore, updates are signed, plugins/DLLs load only from trusted locations.
  • Administrators can control it. Managed policy, kill switch, update channels, diagnostics, audit trail.
  • Everything ships signed, reproducible and with evidence.

2. Before you hand back code

Run these and fix everything they report:

cmake --preset dev && cmake --build --preset dev
ctest --preset dev
clang-format --dry-run --Werror <changed files>
clang-tidy -p build <changed files>
  • Fix a warning rather than silencing it. A // NOLINT needs a reason on the narrowest line; a blanket // NOLINTNEXTLINE or a disabled check in .clang-tidy is a finding.
  • Lint config lives once, in lints/.clang-tidy and lints/.clang-format. If a project has none, suggest the BeautifulQt drop-in of https://github.com/Facta-Dev/beautifulqt.
  • Test the release build on the reference floor machine, not only a dev build on a workstation.

3. The GUI thread and threading

  • No blocking call on the GUI thread. No synchronous network, no large file read, no QProcess::waitForFinished, no QThread::sleep, no heavy parse. If it can take more than a frame, move it off.
  • Use QFuture/QtConcurrent for one-shot work, a worker QObject moved to a QThread for long-lived work, and QThreadPool for bounded tasks.
  • Never touch a widget, model or QObject owned by another thread. Update the UI with a queued signal/slot connection or QMetaObject::invokeMethod with Qt::QueuedConnection.
  • Model updates happen on the model's thread. A background fetch returns data; the ViewModel applies it on the GUI thread.
  • Cancel and coalesce. A newer request cancels or supersedes the older one; a stale result never overwrites a newer state.
  • Exceptions never cross a slot boundary. Use Result<T>/std::expected for expected failures; a thrown exception from a slot is a crash.
  • Timers are UI-paced. A QTimer for debounce/animation is fine; polling and retries live in the application layer with a Clock port.

4. Architecture: hexagonal + vertical slices + MVVM

LayerContainsMay depend onMust not depend on
domainEntities, value objects, invariants, domain events, pure servicesstd, shared-kernel; QtCore value types only if the ADR allowsQObject, signals/slots, QtGui/Widgets/Quick/Network/Sql, OS APIs
applicationUse cases, ports, policy resolver, sync, outbox, permission checksdomain, contracts, QtCore (QObject, QFuture, QTimer)QtGui/Widgets/Quick/Network/Sql/WebSockets, platform adapters
adapters/outSDK, realtime, SQLite, keystore, notifications, updater, telemetry, policyapplication ports, platform, Qt Network/Sql/WebSocketsviews, viewmodels
adapters/inViewModels, realtime event handlers, OS event handlersapplication, contractsother slices' internals, SQL/HTTP
presentation/viewsWidgets or QMLViewModel types, ui-kitSDK, storage, network, application directly
platformPer-OS code behind interfacesOS APIs, Qt Core/GUIdomain/application
contractsDTOs, error codes, event envelope, policy schema, route tableshared-kerneleverything else

The contracts below are unbreakable and non-waivable: they are the reason the app stays fast on old hardware and reviewable at year two. Projects can restyle anything; developers cannot opt out, and neither can you.

  1. The GUI thread never blocks. No synchronous network, disk, parse or crypto on it.
  2. #ifdef Q_OS_* appears only under platform/ (and build files).
  3. Views never touch the SDK, storage or network. They bind to a ViewModel and forward events.
  4. ViewModels never touch widgets, windows or QML. Navigation is by route through a Navigator port.
  5. Errors are values. A Result<T> crosses layers; exceptions do not.
  6. The backend is the source of truth. The local cache is a projection; the outbox is idempotent.

When a change would cross a layer, re-cut the layout: move the logic to the layer that owns it. The rule is never to weaken or delete the lints to make a change pass; a config exception is a contract change. Review treats a broken contract as an automatic Must fix; qt-review ranks it above every other finding.

5. Widgets vs Qt Quick

  • Qt Widgets is the default for data-dense/enterprise screens: CPU raster rendering, predictable on old drivers, no GPU dependency.
  • Qt Quick only for isolated, motion-heavy surfaces, restricted to a software-safe subset; test it with the software backend.
  • No Qt WebEngine in the main app. It adds a Chromium-sized footprint and memory, which breaks the low-end budget; if unavoidable, isolate it in an optional component.
  • One ViewModel contract serves Widgets and QML; the view technology is a replaceable adapter.

6. MVVM in Qt

  • ViewModel = QObject with bindable Q_PROPERTYs (Q_OBJECT_BINDABLE_PROPERTY), invokable commands, and one status enum: Loading | Ready | Empty | Forbidden | NotFound | Offline | Error | PolicyBlocked.
  • Lists expose a QAbstractItemModel (or a proxy), not a QList copied into a widget. The model owns the rows; the ViewModel owns the query and the status.
  • Views connect signals or bind declaratively; no business condition beyond "show this status".
  • ViewModels depend on use cases/ports, a Navigator and a Notifier, never on widgets or windows.
  • One ViewModel per screen or significant component; no global mutable singletons; construction through the composition root.
  • Test ViewModels without a GUI: QSignalSpy, fake ports, no QApplication window.

7. Memory, ownership and lifetimes

  • RAII and value semantics by default. std::unique_ptr for exclusive ownership, QObject parent-child for widgets and QObject trees, QPointer for a non-owning guard.
  • A QObject child dies with its parent. Do not delete a child that has a parent; do not keep a raw pointer to a deleted object.
  • Disconnect or scope connections. A lambda connection that captures this must use the context-object overload so it disconnects when the receiver dies.
  • No dangling references across async boundaries. Capture values or a QPointer, not this blindly.
  • Model indices are transient. A QModelIndex can be invalidated by a reset; persist the id, re-resolve the index.
  • Copy-on-write containers are cheap to copy, not free to mutate. Avoid detaching in a hot loop.

8. Backend, SDK and realtime

  • The generated C++ SDK mirrors the URL contract; it is the only thing that talks to the API. Use QNetworkAccessManager behind the SDK, not in a view.
  • Auth uses OIDC + PKCE in the system browser with a loopback or custom-scheme redirect; tokens in the OS keystore; refresh with reuse detection.
  • Errors are typed values matching the shared envelope ({ error: { code, message, requiredPermission?, correlationId } }).
  • Realtime is a port with SSE, WebSocket and polling adapters; the same state machine, snapshot + stream, dedupe by event id, resume, heartbeats, and a polling fallback.
  • Long operations (202) are polled or streamed with backoff and a timeout; the UI shows progress; cancellation is a command.
  • Idempotency keys on mutations; retries are bounded and safe.

9. Storage and outbox

  • SQLite (WAL) through one storage adapter; a single connection/thread policy; migrations are versioned and tested.
  • The local store is a projection, not authority; a server refresh replaces it.
  • The outbox is persisted, drained on startup/reconnect/timer, with an idempotency key, backoff, and a dead-letter view for admins.
  • A conflict is resolved by the server; the local pending change is rejected visibly.
  • Secrets never go in SQLite; they live in the OS keystore.

10. Security

  • The local machine is untrusted. Verify update signatures, load plugins only from trusted paths, and never eval remote content.
  • No secret in logs, crash dumps, config or the binary. Tokens live in the keystore.
  • TLS is always on, with a custom CA bundle where an enterprise requires it; no --insecure default.
  • Authorization is server-side; the UI hides actions, it does not decide access.
  • Managed policy wins over user settings; a kill switch is applied immediately.
  • Audit trail for admin actions; diagnostics are redacted and opt-in.

11. Performance

  • Startup is a feature: lazy-load heavy screens, defer non-critical work, measure cold start on the floor.
  • Models virtualize: a view over 100k rows uses a model and a proxy, not a widget per row.
  • Images and icons are sized for the target DPI; no giant pixmaps scaled at paint time.
  • Layouts are cheap: avoid deep nesting, repeated sizeHint work, and per-paint allocations in paintEvent.
  • Budgets in CI on the reference machine: startup, screen switch, scroll FPS, memory. A regression fails the build.
  • No WebEngine in the main app; no GPU assumption.

12. Accessibility and internationalization

  • Keyboard is a first-class path: tab order, mnemonics, shortcuts, focus visibility; every action reachable without a mouse.
  • Accessible names and roles via QAccessible; images have text alternatives; errors are announced.
  • Translations with tr() and .ts files; no concatenated sentences; layouts survive long translations and RTL.
  • High DPI and scaling work; no hard-coded pixel sizes around text.
  • Color is never the only signal; contrast and focus rings meet the platform guidance.

13. Testing

  • Unit: domain rules, use cases, the policy resolver, the outbox, the SDK error mapping.
  • ViewModel: QSignalSpy over a fake port; no GUI window.
  • Model: row insert/remove/reset, sorting/filtering, index stability.
  • Integration: the SQLite adapter against a real file/WAL; migrations against fixtures of every released version.
  • GUI: Qt Test with QTest::keyClick/mouseClick; a screenshot check only where it earns its maintenance.
  • Performance: startup/scroll/memory on the reference floor; a regression fails CI.
  • Platform: the platform/ adapters on Windows, Linux and macOS.

14. Cross-skill contract

  • qt-review reviews a change and ranks a broken architecture contract above every other finding.
  • qt-port ports a Qt5/QML/PySide/Electron/web screen to Qt 6 C++ with the same behavior and tests first.
  • The lints/ config is the machine-checkable half. When this skill and the config disagree, the config wins and this file is fixed.

Sources

  • references/qt6.md — the platform digest: Qt 6 LTS, Widgets vs Quick, threading, model/view, QFuture, CMake, packaging, accessibility.
  • references/qt-patterns.md — bindable ViewModels, models, async use cases, the outbox, the realtime port, single-instance apps.
  • references/qt-architecture.md — the layer contract, ports, CMake targets and the enforcement map.
  • references/cpp-strict.md — C++20, RAII, ownership, Result, warnings, and the boundary rules.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.