English French [qt-devnet.developpez.com] 日本語

Alternative Combine

We call this program combine_alter.py. Actually it does no more and no less than the combine.py program, though it does it somewhat differently – it circumvents the multiple inheritance. Though multiple inheritance is a nice Python feature, many programmers, particularly those migrating from other language may well prefer to avoid it, because Python is almost unique in its support of multiple inheritance (note that C++ does support it, but not Java, C# or other usual object-oriented languages). Though we started of with multiple inheritance, some may well prefer this or some other alternative way. By using the name of combine_alter.py we show that its difference from combine.py by appending “_alter” to the name. The combine.ui and qrc_combine.py programs remain unaltered, so we simply use them without bothering to make it available from an upload. The program itself is available from the usual place

All current source code for “Close”, “About”, “Show Licence”, all versions of “Combined”, “Engineering Application” aka “truss” are stored in one repository tuts4pyside. For your convenience, please install a copy of the repository with the following command:

  1.   git  clone  https://github.com/OldAl/tuts4pyside

Program listing follows.
  1. #!/usr/bin/env python
  2. # combine_alter.py - combination of ShowGPL, About, Close scripts
  3.  
  4. import sys
  5. import platform
  6.  
  7. import PySide
  8.                          QMessageBox,  QIcon
  9.  
  10. __version__ = '0.0.0'
  11. from ui_combine import Ui_MainWindow as Ui
  12. import qrc_combine
  13.  
  14. class MainWindow(QMainWindow):
  15.     def __init__(self, parent=None):
  16.         super(MainWindow, self).__init__(parent)
  17.         # Store Ui() as class variable self.ui
  18.         self.ui =  Ui()
  19.         ui.setupUi(self)
  20.         ui.actionShow_GPL.triggered.connect(self.showGPL)
  21.         ui.action_About.triggered.connect(self.about)        
  22.         iconToolBar = self.addToolBar("iconBar.png")
  23. #------------------------------------------------------
  24. # Add icons to appear in tool bar - step 1
  25.         ui.actionShow_GPL.setIcon(QIcon(":/showgpl.png"))
  26.         ui.action_About.setIcon(QIcon(":/about.png"))
  27.         ui.action_Close.setIcon(QIcon(":/quit.png"))
  28. #------------------------------------------------------
  29. # Show a tip on the Status Bar - step 2
  30.         ui.actionShow_GPL.setStatusTip("Show GPL Licence")
  31.         ui.action_About.setStatusTip("Pop up the About dialog.")
  32.         ui.action_Close.setStatusTip("Close the program.")
  33. #------------------------------------------------------        
  34.         iconToolBar.addAction(ui.actionShow_GPL)
  35.         iconToolBar.addAction(ui.action_About)
  36.         iconToolBar.addAction(ui.action_Close)
  37.        
  38.     def showGPL(self):
  39.         '''Read and display GPL licence.'''
  40.         self.ui.textEdit.setText(open('COPYING.txt').read())
  41.        
  42.     def about(self):
  43.         '''Popup a box with about message.'''
  44.         QMessageBox.about(self, "About PyQt, Platform and the like",
  45.                 """<b> About this program </b> v %s
  46.                <p>Copyright 2010 Joe Bloggs.
  47.                All rights reserved in accordance with
  48.                GPL v2 or later - NO WARRANTIES!
  49.                <p>This application can be used for
  50.                displaying OS and platform details.
  51.                <p>Python %s -  PySide version %s - Qt version %s on %s""" % \
  52.                 (__version__, platform.python_version(), PySide.__version__,\
  53.                  PySide.QtCore.__version__, platform.system()))      
  54.        
  55. if __name__ == '__main__':
  56.     app = QApplication(sys.argv)
  57.     frame = MainWindow()
  58.     frame.show()
  59.     app.exec_()

Basically, it is a matter of taste which method one uses – with multiple inheritance or without. Anyhow, it may well be easier ot adopt one or the other. Even though, it is good to have some familiarity with both methods, as one meets both when reading programs.

Enjoy the slightly different presentation of this very basic program setup! Return to PySideSimplicissimus

Categories: