Thursday, June 17, 2021

How to Generate a Qt Designer UI Code from the Command Line

Sometimes it's easier to work with UI code when you generate it from the command line.  It's quick, easy to understand, and is easy to code the rest of your functionality with.  The command line is a very powerful tool, for me personally this was the first time getting a taste as to how powerful it really is.  When working with the command line to generate keep in mind that when you work it the code, you will have to clean and organize code  so that it is readable, the bigger and more functionality the UI has the bigger and more clean up you will have to do, and if you need to change something you will have to regenerate depending on what the change is.  The code generated does look different from just importing it so take your time to read through it to fully understand what it's doing.  To start your process on generating have an organized location you are saving your file to and have a UI created in Qt Designer.  

UI Created in Qt Designer

Save your UI and navigate to where your UI is saved in your folder structure.  You need to navigate to your folder structure because the command line is going to take that location when you generate the code. 

Navigating to UI in Folder Structure

In your folder directory, in type cmd and the command line window should pop up. When you type cmd in any directory it will create your UI code in that location. 

Gif of using the File Directory for the Command Line

Once you are in the command line, type in pyuic5 -x nameOfUI.ui -o nameOf Code.py.  This code tells the command line to extract the code from the ui file and open it as a python code file. 

Code in Command Line

Hit enter and you should see the command line update waiting for the next command.

Updated Command Line

From here you can look back in your file directory and see that your code has generated!

Example Code in File Directory

When you open the code it should look something like this.  For the codes I generate I always run the code and make sure everything is working before I start working.  I then go in and split the code so that it is organized and easy to follow with comments on what the code is.  

Example Code Generated with no Clean Up

No comments:

Post a Comment

Zenko Rig Reel

Zenko Rig Reel Zenko was my capstone character that I rigged and animated for Zenko: A Fox's Tale.  He is built to have a very reactive ...