Qt Creator : how to invoke uic automatically ?
-
Hello,
I can't figure out how to automatically convert Qt Designer files to Python code directly from Qt Creator, as can be done in C++.
I tested PySide6's
uic.exe
in the command line, and it works perfectly :$ uic -g python form.ui > ui_form.py
I tried to set the full path to
uic.exe
in the Run PySide6 project tool text box in Projects > Build > Build steps section, butuic.exe
gets called with the following command line, which doesn't make sense :"PATH_TO_CONDA_ENVNAME\Library\lib\qt6\uic.exe" build
I couldn't find any clue on the web, so I wonder if this feature is available yet. Can anyone help ?
Platform :
- Windows 11
- Qt Creator : 9.0.2
- PySide6
- Python : 3.9
- Miniconda3 : conda 23.1.0
-
@dpcrd
Since I don't know (when I used Python I did theuic
invocation outside of Creator), I decided to try the trusty ChatGPT :)[Me:] how do you invoke
uic
for Python from within Qt CreatorTo invoke the uic command in Qt Creator for Python, you can follow these steps:
Open your .ui file in Qt Creator.
Click on the "Design" button in the bottom left corner of the editor.
Click on "Form" in the top left corner of the editor.
Select "Python" in the "Class Information" section on the right side of the editor.
Click on the "Generate Form and Control File" button, which looks like a gear icon with a python logo on it. This will open a dialog box.
In the dialog box, make sure the "Use automatic path" option is selected, and choose the destination folder for the generated files.
Click "Generate" to generate the Python files from your .ui file.
This will create two Python files, one containing the UI code and one containing the main window code. You can then use these files in your Python project by importing them as modules.Note that the uic command is automatically invoked by Qt Creator when you generate the Python files from the .ui file, so you do not need to invoke it separately.
[Me:] How can you invoke the
uic
command as part of the Build steps instead of having to click on things?
!
Something went wrong. If this issue persists please contact us through our help center at help.openai.com.As you can see, asking it how to do it from some kind of "Build" was too much for it to handle ;-)
Don't know if that's of any use? I suspect not...? :(
UPDATE
Upon giving it another chance on that second question, it did come up with:2 / 2
To invoke the uic command as part of the build process in Qt Creator, you can add a custom build step to your project file. Here are the steps:
Open your project file (.pro) in Qt Creator.
Add the following lines to your project file to specify the input .ui file and the output .py file:
bash
Copy codeUI_FILE = path/to/your/ui/file.ui PY_FILE = path/to/your/python/file.py
Add a custom build step to the project file to run the uic command. The build step should look like this:
bash
Copy codeuic -o ${PY_FILE} ${UI_FILE}
This command will generate the Python code for your UI file and save it to the specified output file.
Save your project file.
Open the "Projects" pane in Qt Creator, and click on the "Build & Run" tab.
In the "Build Steps" section, click the "+" button to add a new build step.
Select "Custom Process Step" from the dropdown menu.
In the "Command" field, enter the command to run make or ninja, depending on your build system.
In the "Arguments" field, enter the name of the target to build, or leave it blank to build all targets.
Click on the "Add Build Step" button to add the custom build step.
Now, when you build your project, Qt Creator will automatically run the uic command to generate the Python code for your UI file, before building the rest of your project. This will ensure that your Python code is up-to-date with any changes you make to your UI file.
[End of conversation.]
[The
uic
command might require the-g
option for Python though?] Now, does that help?! -
@JonB
Well, Qt Creator creates a.pyproject
file, which is a JSON file, not a.pro
file and I didn't know how to declare those variables. Plus, I'd have to declare them for every form and add a build step for every form. Maybe CMake could help do something cleaner, but I didn't find anything on the web.Anyway, I found a way to do what I wanted with a Bash script (I don't know Windows shell scripting that much). Git must be installed. I guess I'll do the same for the resource compiler. Here is my script below.
The comments at the top of the script explain how to use it. I guess it can be used on Linux too, by changing the command and the arguments in the Qt Creator custom build step.
#!/usr/bin/bash # Synopsis : call Qt's UI compiler on every .ui file in the current Python project directory. # # The working directory must be the project source directory. # Add a custom build step in Qt Creator and point to the script. # On Windows : # - command : full path to Git's Bash, e.g. C:\Program Files\Git\bin\bash.exe # - arguments : # - full path to this script # - Linux-style full path to uic.exe # - example of the "arguments" field : C:\Users\admin\bin\uic.sh /c/Users/admin/miniconda3/envs/atlas-ids/Library/lib/qt6/uic.exe # - Working directory : %{sourceDir} function usage() { echo -e "Usage: $(basename $0) UIC [-h]\n" echo -e "UIC\tFull path to Qt's user interface compiler uic.exe" echo -e "-h\tDisplay this message and quit" } if [ $# -lt 1 ]; then echo "ERROR >> Missing argument." usage exit 1 fi if [ $1 == "-h" ]; then usage exit 0 fi # Get path to UI compiler from 1st argument : UIC=$1 if [ ! -f $UIC ]; then echo "ERROR >> UI compiler not found : $UIC" exit 1 fi for f in *.ui do echo "Compiling $f" $UIC -g python $f -o ui_${f%.ui}.py done exit 0
@JonB : I love your way of "solving" problems 😂 ! Why didn't I ask some AI to do my job ? But wait ! How about my wages, then ? 🤔
-
@dpcrd said in Qt Creator : how to invoke uic automatically ?:
@JonB : I love your way of "solving" problems 😂 ! Why didn't I ask some AI to do my job ? But wait ! How about my wages, then ? 🤔
I very rarely refer to ChatGPT. I did on this occasion because I did not know the answer and had trouble finding it.
Your script always runs
uic
on every.ui
file. If properly integrated into the build process it would only run it where.ui
file timestamp newer than generatedui_....py
file. However I guess it does not take too long.