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.
In this repository24 other files it gives its agents.
- .claude/CLAUDE.mdCLAUDE.md
- CLAUDE.mdCLAUDE.md
- add-linux-packagingSkill
- add-sliceSkill
- add-viewSkill
- claim-checkSkill
- clean-room-rebuildSkill
- corpus-retrievalSkill
- diagnosing-bugsSkill
- domain-modelingSkill
- graphifySkill
- grillingSkill
- learnSkill
- planSkill
- platform-portSkill
- researchSkill
- reviewSkill
- shipSkill
- specSkill
- tddSkill
- verified-statusSkill
- visual-reviewSkill
- qt-portSkill
- qt-reviewSkill
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
- Distinctive: line 32
Budget first, features second: Every release is measured against a low-end reference machine.
- Distinctive: line 51
Run these and fix everything they report:
- Distinctive: line 66
Test the release build on the reference floor machine, not only a dev build on a workstation.
- Very specific: line 71
No blocking call on the GUI thread: No synchronous network, no large file read, no QProcess::waitForFinished, no QThread::sleep, no heavy …
- Very specific: line 83
Exceptions never cross a slot boundary: Use Result<T>/std::expected for expected failures.
- Distinctive: line 104
The GUI thread never blocks: No synchronous network, disk, parse or crypto on it.
- Distinctive: line 115
When a change would cross a layer, re-cut the layout: move the logic to the layer that owns it.
- Very specific: line 156
A
QObjectchild dies with its parent: Do not delete a child that has a parent. - Very specific: line 199
The local machine is untrusted: Verify update signatures, load plugins only from trusted paths, and never eval remote content.
- Very specific: line 219
Layouts are cheap: avoid deep nesting, repeated sizeHint work, and per-paint allocations in paintEvent.
≈ “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.
Files
Qt best practices
Playbook set: start with
PLAYBOOK_MASTER.md— the catalog, precedence and cross-playbook invariants — thenPLAYBOOK_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
// NOLINTneeds a reason on the narrowest line; a blanket// NOLINTNEXTLINEor a disabled check in.clang-tidyis a finding. - Lint config lives once, in
lints/.clang-tidyandlints/.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, noQThread::sleep, no heavy parse. If it can take more than a frame, move it off. - Use
QFuture/QtConcurrentfor one-shot work, a workerQObjectmoved to aQThreadfor long-lived work, andQThreadPoolfor bounded tasks. - Never touch a widget, model or QObject owned by another thread. Update
the UI with a queued signal/slot connection or
QMetaObject::invokeMethodwithQt::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::expectedfor expected failures; a thrown exception from a slot is a crash. - Timers are UI-paced. A
QTimerfor debounce/animation is fine; polling and retries live in the application layer with aClockport.
4. Architecture: hexagonal + vertical slices + MVVM
| Layer | Contains | May depend on | Must not depend on |
| domain | Entities, value objects, invariants, domain events, pure services | std, shared-kernel; QtCore value types only if the ADR allows | QObject, signals/slots, QtGui/Widgets/Quick/Network/Sql, OS APIs |
| application | Use cases, ports, policy resolver, sync, outbox, permission checks | domain, contracts, QtCore (QObject, QFuture, QTimer) | QtGui/Widgets/Quick/Network/Sql/WebSockets, platform adapters |
| adapters/out | SDK, realtime, SQLite, keystore, notifications, updater, telemetry, policy | application ports, platform, Qt Network/Sql/WebSockets | views, viewmodels |
| adapters/in | ViewModels, realtime event handlers, OS event handlers | application, contracts | other slices' internals, SQL/HTTP |
| presentation/views | Widgets or QML | ViewModel types, ui-kit | SDK, storage, network, application directly |
| platform | Per-OS code behind interfaces | OS APIs, Qt Core/GUI | domain/application |
| contracts | DTOs, error codes, event envelope, policy schema, route table | shared-kernel | everything 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.
- The GUI thread never blocks. No synchronous network, disk, parse or crypto on it.
#ifdef Q_OS_*appears only underplatform/(and build files).- Views never touch the SDK, storage or network. They bind to a ViewModel and forward events.
- ViewModels never touch widgets, windows or QML. Navigation is by route
through a
Navigatorport. - Errors are values. A
Result<T>crosses layers; exceptions do not. - 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 =
QObjectwith bindableQ_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 aQListcopied 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
Navigatorand aNotifier, 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, noQApplicationwindow.
7. Memory, ownership and lifetimes
- RAII and value semantics by default.
std::unique_ptrfor exclusive ownership,QObjectparent-child for widgets and QObject trees,QPointerfor a non-owning guard. - A
QObjectchild dies with its parent. Do notdeletea child that has a parent; do not keep a raw pointer to a deleted object. - Disconnect or scope connections. A lambda connection that captures
thismust use the context-object overload so it disconnects when the receiver dies. - No dangling references across async boundaries. Capture values or a
QPointer, notthisblindly. - Model indices are transient. A
QModelIndexcan 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
QNetworkAccessManagerbehind 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
evalremote 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
--insecuredefault. - 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
sizeHintwork, and per-paint allocations inpaintEvent. - 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.tsfiles; 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:
QSignalSpyover 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-reviewreviews a change and ranks a broken architecture contract above every other finding.qt-portports 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.
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
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.

