PyQt dynamic uic example

Here is a minimal example for PyQt4, to set up a main window and connect a simple action to some slot. The documentation on doing this is sparse to say the least. So I hope this is helpful for anyone trying something similar. I assume that you have created a ui_MainWindow.ui file with Qt Designer, which contains a form derived from QMainWindow.

import sys
from PyQt4 import QtCore, QtGui, uic

class MyMainWindow(QtGui.QMainWindow):

def __init__(self):
self.ui = uic.loadUi("ui_MainWindow.ui", self)
self.connect(self.ui.actionOpen, QtCore.SIGNAL('triggered()'), self, QtCore.SLOT('open()'))

def open(self):
fileDialog = QtGui.QFileDialog(self, "Open file")

app = QtGui.QApplication(sys.argv)
mainWindow = MyMainWindow()

1 comment: