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

Show Licence

Please note that this example shows the CCPL license, whereas PySide is released under LGPL. So make sure that you are displaying the right license.

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

If you do not have git, pllease install it first.

After downloading, place the CCPL.txt in the same directory as the program. The file has the Creative Commons Attribution-Share alike Licence text. licence.ui needs to be converted to Python file as follows:

  1. pyside-uic licence.ui > ui_licence.py

Program Listing: follows:
  1. #!/usr/bin/env python
  2. # licence.py - display GPL licence
  3.  
  4. import sys
  5.  
  6.  
  7. from ui_licence import Ui_MainWindow
  8.  
  9. class MainWindow(QMainWindow, Ui_MainWindow):
  10.     def __init__(self, parent=None):
  11.         '''Mandatory initialisation of a class.'''
  12.         super(MainWindow, self).__init__(parent)
  13.         self.setupUi(self)
  14.         self.showButton.clicked.connect(self.fileRead)
  15.        
  16.     def fileRead(self):
  17.         '''Read and display GPL licence.'''
  18.         self.textEdit.setText(open('COPYING.txt').read())
  19.        
  20. if __name__ == '__main__':
  21.     app = QApplication(sys.argv)
  22.     frame = MainWindow()
  23.     frame.show()
  24.     app.exec_()

If you run the program and click the button, it will show in the TextEdit window the listing of the licence. All of the code mimics closely that of About and Close scripts. Two statements are of some interest:
  1. self.showButton.clicked.connect(self.fileRead)

The purpose of the pushButton is to show the licence, so it has been named showButton. The above statement connects the showButton.clicked event with a class method fileRead. Thus when showButton is clicked, the Python class method fileRead is executed. That method has a single “Pythonised” statement:
  1. self.textEdit.setText(open('COPYING.txt').read())

Not the simplest way to show what happens, but Python programmers like to do such things to us, readers. The code is equivalent to a series of statements as follows:
  1. #open file
  2. fl = open('COPYING.txt')
  3. tmp = fl.read()
  4. self.textEdit.setText(tmp)

It may be clearer, but a lot more typing and somewhat slower as Python is an interpreter. It is a subjective judgement which of the two styles is better.

Return to PySideSimplicissimus

Categories: