Part 3: Multiple Pages and Nested UIs
With a bulk of the basics covered, this post focuses on two concepts: handling multiple pages within a UI, and putting one .ui file into another. I am going to skip some of the basics, as they are covered in the previous posts.
Multi Page UI:
The QT Designer setup for a mutli page UI is actually surprisingly simple, all I need to do is use a QStackedWidget:
Each page contains a continue button, which will load the next page in the QStackedWidget, and any other UI elements which are located within it.
The button setup is relatively simple as well in our python file, we just need to connect these functions to the appropriate buttons to go back and forth between the two pages:
We can also use these functions to manage what we do on each page, such as using this function to connect the buttons, setting the text on page two when we go to load it, or populating an array of UI elements such as the next section covers.
That's really all there is to it on multi-page UIs, just use a QStackedWidget and make sure something changes the current index of it.
One of the ideas I knew I wanted to have was nesting UI files within other UIs, and fortunately it's not too complicated! In the above screenshot I have a QScrollArea named scrollArea_p2, I'm going to put an array of a second UI class inside of it!
Here is the nested UI object I plan to use:
Note that instead of a main window as my top-level object, it is just a QFrame. I chose it because I knew this wouldn't be a stand alone window, but rather a child of another UI window.
The python class itself for this UI is rather minimal as well, as I will actually be connecting the buttons within my main UI's class:
With the class made, in my main UI class I have created this function to load the nested UI elements:
Line 5: Loop through the list of ObjectNames to create UIs for each entry. I probably could have been fancy and used enumerate but oh well.
Lines 8-9: Get the height of the new UI object, and move it according to what position it should be at. A layout could have probably been used, but this is just my preference.
Line 11: Just labeling the button!
Lines 12-13: This is rather interesting, and you'll see if you try this yourself but you need the full throw_away junk whenever hooking functions up within a loop, otherwise interesting things happen.
Line 15: Resize the scroll area so that the scroll bar is correct.
And here it is within Maya:
This demo file is setup to show the interesting aspect of looping through to make UI elements ~ the green buttons will print the correct number, however the orange buttons will only ever print the max value ( value of the last loop ).
Wrapping it up!
Really, we can do almost anything we'd like to with these .ui files, support for multi-page UIs is a breeze and it's not too difficult to nest one .ui file within another. Feel free to dig through the files throughout all of this
Demo Files ( .zip )
To use the demo files:
1) Close Maya if it's open
2) Extract the contents of this.zip into your maya/scripts/ folder
3) Open Maya
4) in Maya, create a python shelf script with the following code:
Press the shelf icon and it should open up!