PyQt4 UIC Module Example
I have been really amazing myself at how much knowledge I have forgotten in the past five or six months… Most of the work I did in the past year utilized the UIC module to load UI files directly, but I can find very little information about this online. I was surprised to see that even the trusty old Rapid GUI Programming with Python and Qt book doesn’t cover loading UI files with the UIC module.
So, here is a tiny script with UI file [download] that will generate a pyqt example window that does ‘stuff’:
import sys from PyQt4 import QtGui, QtCore, uic class TestApp(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.ui = uic.loadUi('X:/projects/2010/python/pyqt_tutorial/pyqt_tutorial.ui') self.ui.show() self.connect(self.ui.doubleSpinBox, QtCore.SIGNAL("valueChanged(double)"), spinFn) self.connect(self.ui.comboBox, QtCore.SIGNAL("currentIndexChanged(QString)"), comboFn) self.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), buttonFn) def spinFn(value): win.ui.doubleSpinBoxLabel.setText('doubleSpinBox is set to ' + str(value)) def buttonFn(): win.ui.setWindowTitle(win.ui.lineEdit.text()) def comboFn(value): win.ui.comboBoxLabel.setText(str(value) + ' is selected') if __name__ == "__main__": app = QtGui.QApplication(sys.argv) win = TestApp() sys.exit(app.exec_()) |
Change the path to reflect where you have saved the UI file, and when you run the script you should get this:
EDIT: A few people have asked me to update this for other situations
PySide Inside Maya:
import sys from PySide.QtUiTools import * from PySide.QtCore import * from PySide.QtGui import * class TestApp(QMainWindow): def __init__(self): QMainWindow.__init__(self) loader = QUiLoader() self.ui = loader.load('c:/pyqt_tutorial.ui') self.ui.show() self.connect(self.ui.doubleSpinBox, SIGNAL("valueChanged(double)"), spinFn) self.connect(self.ui.comboBox, SIGNAL("currentIndexChanged(QString)"), comboFn) self.connect(self.ui.pushButton, SIGNAL("clicked()"), buttonFn) def spinFn(value): win.ui.doubleSpinBoxLabel.setText('doubleSpinBox is set to ' + str(value)) def buttonFn(): win.ui.setWindowTitle(win.ui.lineEdit.text()) def comboFn(value): win.ui.comboBoxLabel.setText(str(value) + ' is selected') win = TestApp() |
PyQT Inside Maya:
import sys from PyQt4 import QtGui, QtCore, uic class TestApp(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.ui = uic.loadUi('c:/pyqt_tutorial.ui') self.ui.show() self.connect(self.ui.doubleSpinBox, QtCore.SIGNAL("valueChanged(double)"), spinFn) self.connect(self.ui.comboBox, QtCore.SIGNAL("currentIndexChanged(QString)"), comboFn) self.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), buttonFn) def spinFn(value): win.ui.doubleSpinBoxLabel.setText('doubleSpinBox is set to ' + str(value)) def buttonFn(): win.ui.setWindowTitle(win.ui.lineEdit.text()) def comboFn(value): win.ui.comboBoxLabel.setText(str(value) + ' is selected') win = TestApp() |
Thanks for this helpful post!
Comment by Angel — 2011/12/16 @ 7:15 PM
Thank you
your post is helpful
Comment by Enu — 2012/12/04 @ 3:30 PM
Thanks a lot!!! I could have never run my app without this post!!!
Thanks again! 🙂
Comment by Ashish Ranjan — 2012/12/19 @ 7:41 AM
Chris,
I copied and pasted the TestApp example (the first one) and it come up with an error;
Traceback (most recent call last):
File “testapp.py”, line 4, in
class TestApp(QtGui.QMainWindow):
File “testapp.py”, line 11, in TestApp
self.connect(self.ui.doubleSpinBox, QtCore.SIGNAL(“valueChanged(double)”), spinFn)
NameError: name ‘self’ is not defined
I don’t understand why.
Ron.
Comment by Ron — 2013/06/27 @ 2:36 PM
Thank you. Helped getting started with QT-Designer and Maya. 🙂
Comment by kogen — 2014/01/06 @ 3:23 PM
Hi Chris — is there a particular way to parent the windows to the main Maya UI? In the example it’s a free floating window.
Comment by john — 2015/07/02 @ 12:04 AM
THANKS A BUNCH!
Comment by John — 2015/07/15 @ 12:42 AM