PySideSimplicissimus Module 7 CombineAllIn1

From Qt Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

English 日本語

Combine All In One

This is the last version of different layout of combine.py program. In this version we will eliminate altogether the combine.ui file and its derivative ui_combine.py file. There is a clear advantage in that - there are fewer files. The disadvantage is that if later we decide to change the appearnce we no longer can have the benefits of using Qt Designer without modifying back the setup. When the program is simply a GUI for an underlying program, it is sometimes worth while to use this modified format. In any case, my favourite GUI design book uses this form, suggesting that it is just as easy to design the forms without visual aids.

As in earlier version, all the required bits of code and other resources are available for a download from the following "repository":

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

The modification of program is implemented by first of all copying the procedure setupUi() from ui_combine_allin1.py into the body of the program itself, combine_allin1.py. Then all the statements are transferred into the procedure init of the main program. That requires several modifications. Fortunately these modifications are very good opportunity to check once understanding of the process of program. We also use this opportunity to determine which widgets we need to import, as we no longer use explicitly the names of QtCore and QtGui in the code. The code looks cleaner then!

Here is the program listing:

#[[Image:/usr/bin/env python
# combine_allin1.py - combination of ShowGPL, About, Close scripts
# The purpose of this version of program is to show implementation 
# of most code in one file - all_in_1|]].

import sys
import platform

import PySide

from PySide.QtCore import QRect
from PySide.QtGui import QApplication, QMainWindow, QTextEdit, QPushButton, QMessageBox, QIcon, QAction, QWidget, QGridLayout, QTextEdit, QMenuBar, QMenu, QStatusBar

''version'' = '0.0.0'
import qrc_combine

class MainWindow(QMainWindow):
 def ''init''(self, parent=None):
 super(MainWindow, self).''init''(parent)
 self.resize(731, 475)
 centralwidget = QWidget(self)
 gridLayout = QGridLayout(centralwidget)
 # textEdit needs to be a class variable.
 self.textEdit = QTextEdit(centralwidget)
 gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
 self.setCentralWidget(centralwidget)
 menubar = QMenuBar(self)
 menubar.setGeometry(QRect(0, 0, 731, 29))
 menu_File = QMenu(menubar)
 self.setMenuBar(menubar)
 statusbar = QStatusBar(self)
 self.setStatusBar(statusbar)
 actionShow_GPL = QAction(self)
 actionShow_GPL.triggered.connect(self.showGPL)
 action_About = QAction(self)
 action_About.triggered.connect(self.about)
 iconToolBar = self.addToolBar("iconBar.png")
#——————————————————
# Add icons to appear in tool bar - step 1
 actionShow_GPL.setIcon(QIcon(":/showgpl.png"))
 action_About.setIcon(QIcon(":/about.png"))
 action_Close = QAction(self)
 action_Close.setCheckable(False)
 action_Close.setObjectName("action_Close")
 action_Close.setIcon(QIcon(":/quit.png"))
#——————————————————
# Show a tip on the Status Bar - step 2
 actionShow_GPL.setStatusTip("Show GPL Licence")
 action_About.setStatusTip("Pop up the About dialog.")
 action_Close.setStatusTip("Close the program.")
#——————————————————
 menu_File.addAction(actionShow_GPL)
 menu_File.addAction(action_About)
 menu_File.addAction(action_Close)
 menubar.addAction(menu_File.menuAction())

iconToolBar.addAction(actionShow_GPL)
 iconToolBar.addAction(action_About)
 iconToolBar.addAction(action_Close)
 action_Close.triggered.connect(self.close)

def showGPL(self):
 '''Read and display GPL licence.'''
 self.textEdit.setText(open('COPYING.txt').read())

def about(self):
 '''Popup a box with about message.'''
 QMessageBox.about(self, "About PyQt, Platform and the like",
 """<b> About this program </b> v %s
 <p>Copyright &copy; 2011 Your Name.
 All rights reserved in accordance with
 GPL v2 or later - NO WARRANTIES!
 <p>This application can be used for
 displaying OS and platform details.
 <p>Python %s - PySide version %s - Qt version %s on s"""  (''version'', platform.python_version(), PySide.''version'', PySide.QtCore.''version'', platform.system()))

if ''name'' == '''main''':
 app = QApplication(sys.argv)
 frame = MainWindow()
 frame.show()
 sys.exit(app.exec_())

No doubt you have noticed that one type of statement is left intact but commented out - these are the statements that include the command setObjetName('name_string'). The reason for commenting it out is that these commands are not used in this example at all - the program executes as well without them. They are, however, not just deleted - since they are the direct result of working with the aid of Qt Designer and conversion with pyside-uic script, perhaps they may be useful for certain types of debugging situations. So it does not cost much (actually it costs nothing!) to just comment them out. It is a kind of compromise.

This is the last variation of combine.py that we explore. I think the time is ripe for us to explore a simple engineering problem and use PySide for the GUI design for the underlying program. The problem is very simple - determination of forces in a truss. The method of doing it and data handling is realistic, but not too extensive, so that it is useful for newbie to newbie tutorials. ¡Hasta la vista!