如何以标准Qt方式创建单个.ui.qml表单文件?
.ui.qml File: Standard Qt Approach Great question! Let's clarify the official, standard ways to create and use a single .ui.qml file (instead of the split Component.qml + ComponentForm.ui.qml setup you're familiar with) — and confirm that this pattern is fully supported by Qt, not a workaround.
First, a quick note: Qt's split file structure is designed for separating UI and business logic (ideal for complex components), but using a single .ui.qml file as a complete, self-contained component is absolutely a valid, standard practice (as shown in the Contact List example). It's perfect for simple components where UI and logic are tightly coupled or minimal.
Standard Ways to Create a Single .ui.qml File
Here are the two official, clean methods in Qt Creator:
Directly Create a
.ui.qmlFile- Right-click your project in the Projects pane →
Add New... - Select
Qt Quick File(notQt Quick UI File) from the Qt Quick category - In the "File name" field, explicitly name your file with the
.ui.qmlextension (e.g.,Contact.ui.qml) - Click
Finish— this generates a single.ui.qmlfile that you can edit in both Qt Quick Designer and the code editor. Qt Creator recognizes the.ui.qmlextension and treats it as a designable UI file, just like the split form files.
- Right-click your project in the Projects pane →
Repurpose the Split File Setup (Optional)
- If you start by creating a
Qt Quick UI File(which generates two files:Component.qmlandComponentForm.ui.qml), you don't need to delete the logic file as a "workaround". Instead:- Move all your business logic directly into the
.ui.qmlfile - Delete the standalone
.qmllogic file if you don't need it
- Move all your business logic directly into the
- This is still a valid approach — Qt doesn't enforce the split pairing; the
.ui.qmlfile works perfectly on its own.
- If you start by creating a
Key Notes
- A single
.ui.qmlfile behaves exactly like a regular QML component: you can import it, instantiate it, and add properties/signals/functions directly inside it. - The
.ui.qmlextension is just a convention to indicate the file is optimized for Qt Quick Designer editing — there's no technical restriction preventing you from adding logic to it.
内容的提问来源于stack exchange,提问作者Bobur

