Sunday, November 30, 2014

PyQt (1): Widget & Layout

Note: 
Qt is a set of C++ libraries and is widely used in UI design on different OS platforms. 
PyQt4 implements most of parts of these classes as a set of Python modules.
Here we only discuss PyQt for Maya.

####################################################################

There are many different types of widgets and layouts in PyQt.

Widget is a house, containing all the UI elements.
Layout is the floor plan for the house, which controls how the UI looks like. 

Here only introduce the most basic widget, QWidget
and two most basic layouts, QVBoxLayout and QHBoxLayout.
(There is QLayout in Qt Library, but it is an abstract class and cannot be instantiated)

QVBoxLayout will vertically layout UI elements;
QHBoxLayout will horizontally layout UI elements.

Example:
# if user uses Maya2014 or above, import from PySide
# if Maya version is below 2014, then import from PyQt4 if PyQt4 Library installed
try:
    from PySide import QtGui
except:
    from PyQt4 import QtGui

# create widget and its layout
widget = QtGui.QWidget()
layout = QtGui.QHBoxLayout()
widget.setLayout(layout)

# create two buttons
button1 = QtGui.QPushButton('Button 1')
button2 = QtGui.QPushButton('Button 2')

# add buttons to layout
layout.addWidget(button1)
layout.addWidget(button2)

# show the window
widget.show()

No comments:

Post a Comment