如何在Qt Designer中添加自定义QDialog子类模板及解决提升无选项问题?
Solutions for Using Your Custom QDialog Subclass in Qt Designer
Let's break down fixes for both of your issues—no manual .ui file edits required, which is exactly what you're looking for:
1. Adding Your Custom Dialog to the File/New Template List
Qt Creator relies on template files and configuration to populate the File/New menu. Here's how to add your custom dialog as a reusable, selectable template:
- First, create a base .ui template in Qt Designer:
- Open Qt Designer, start with a new QDialog form, set up any default widgets or layouts you want for your custom dialog (or leave it blank), then save it as
CustomDialogTemplate.ui.
- Open Qt Designer, start with a new QDialog form, set up any default widgets or layouts you want for your custom dialog (or leave it blank), then save it as
- Locate the template directories (use the user-specific folder to avoid modifying Qt's system files):
- Windows:
%APPDATA%\QtProject\qtcreator\templates\wizards\forms - Linux:
~/.config/QtProject/qtcreator/templates/wizards/forms - macOS:
~/Library/Preferences/QtProject/qtcreator/templates/wizards/forms - If the
formssubfolder doesn't exist, create it.
- Windows:
- Make a new subfolder (e.g.,
custom_dialog) inside theformsdirectory, then move yourCustomDialogTemplate.uiinto it. - Add a
wizard.xmlfile to this subfolder (adjust values to match your custom class):<?xml version="1.0" encoding="UTF-8"?> <Wizard version="1"> <Name>Custom Dialog</Name> <Description>Create a dialog based on your custom QDialog subclass</Description> <Icon>:/icons/dialog.png</Icon> <!-- Use Qt's built-in icon or a custom file path --> <Category>Forms</Category> <Files> <File source="CustomDialogTemplate.ui" target="%{JS: Util.fileName('%{Class}.ui', '')}" /> </Files> <ClassSettings> <Class name="%{Class}" baseClass="YourCustomDialogClassName" /> <HeaderFile>%{Class}.h</HeaderFile> <SourceFile>%{Class}.cpp</SourceFile> </ClassSettings> </Wizard> - Restart Qt Creator. You'll now see your "Custom Dialog" option under the Forms category when you navigate to File > New.
2. Fixing the Missing "Promote to..." Right-Click Option
If the promote option is nowhere to be found, try these targeted fixes:
- Select the correct node: The "Promote to..." option only appears when you right-click the top-level QDialog entry in the Object Inspector (the panel on the right that lists all widgets in the form). Right-clicking directly in the editor area won't show this option—make sure you're interacting with the root dialog node in the inspector.
- Ensure your class is indexed: Compile your project at least once so Qt Creator's code model can recognize your custom QDialog subclass. If the class isn't indexed, Qt Designer won't know it exists to promote to.
- Manually add the promoted class (if needed):
- In Qt Designer, go to Tools > Promoted Widgets.
- Click Add, then enter your custom class name in the "Promoted class name" field, and its header file path (e.g.,
yourcustomdialog.h) in the "Header file" field. - Click Add again, then Close.
- Back in the Object Inspector, select the top-level QDialog, scroll to the bottom of the Property Editor, and use the "Promoted Widgets" dropdown to select your custom class, then click Promote.
All these steps work without touching the generated .ui file directly.
内容的提问来源于stack exchange,提问作者JoeFromVienna




