Posted By

blackthorne on 02/04/11


Tagged

python gui ui Designer pyqt Qt


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

blackthorne


Qt GUI app in python with pyqt (subclassing pyuic4 generated file)


 / Published in: Python
 

first design your app and generate a .UI file using Designer (part of Qt Creator), then run pyuic4 to get it to python, like: pyuic4-2.6 example.ui > example.py

then import the window classes in example.py and make your up by subclassing like you can see in the source

  1. import sys
  2.  
  3. from PyQt4.QtCore import *
  4. from PyQt4.QtGui import *
  5.  
  6. from <REPLACE_BY_PYUI_DESIGN> import <REPLACE_BY_WINDOW_CLASS_IN_PYUI_DESIGN>
  7.  
  8. class MainWindow(QMainWindow, <REPLACE_BY_WINDOW_CLASS_IN_PYUI_DESIGN>):
  9.  
  10. # custom slot
  11. def mymethod(self):
  12. self.textFieldExample.setText('Hello World')
  13. self.textFieldExample.clear()
  14.  
  15. def __init__(self):
  16. QMainWindow.__init__(self)
  17.  
  18. # set up User Interface (widgets, layout...)
  19. self.setupUi(self)
  20. # custom slots connections
  21. QObject.connect(self.pushButton,SIGNAL("released()"),self.mymethod) # signal/slot connection
  22.  
  23. # Main entry to program. Sets up the main app and create a new window.
  24. def main(argv):
  25.  
  26. # create Qt application
  27. app = QApplication(argv,True)
  28.  
  29. # create main window
  30. wnd = MainWindow() # classname
  31. wnd.show()
  32.  
  33. # Connect signal for app finish
  34. app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
  35.  
  36. # Start the app up
  37. sys.exit(app.exec_())
  38.  
  39. if __name__ == "__main__":
  40. main(sys.argv)

Report this snippet  

You need to login to post a comment.