PySideSimplicissimus Module 7 CombineAllIn1: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[PySideSimplicissimus Module 7 CombineAllIn1 Japanese|日本語]]
[[Category:LanguageBindings]]<br />[[Category:LanguageBindings::PySide::Newbie Tutorials]]


* '''Note:''' this article is a member of the multipart [[PySide-Newbie-Tutorials|PySide Newbie Tutorials]]
'''English''' [[PySideSimplicissimus_Module_7_CombineAllIn1_Japanese|日本語]]


=Combine All In One=
* '''Note:''' this article is a member of the multipart [[PySide-Newbie-Tutorials]]


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 <span class="caps">GUI</span> for an underlying program, it is sometimes worth while to use this modified format. In any case, my favourite <span class="caps">GUI</span> design book uses this form, suggesting that it is just as easy to design the forms without visual aids.
= Combine All In One =


As in earlier version, all the required bits of code and other resources are available for a download from the following “repository”:<br />
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 &quot;repository&amp;quot;:<br /><code> git clone https://github.com/OldAl/tuts4pyside<code>


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!
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!
Line 13: Line 15:
Here is the program listing:
Here is the program listing:


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.
</code><br />#[[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<br />import platform
 
import PySide
 
from PySide.QtCore import QRect<br />from PySide.QtGui import QApplication, QMainWindow, QTextEdit, QPushButton, QMessageBox, QIcon, QAction, QWidget, QGridLayout, QTextEdit, QMenuBar, QMenu, QStatusBar
 
''version'' = '0.0.0'<br />import qrc_combine
 
class MainWindow(QMainWindow):<br /> def ''init''(self, parent=None):<br /> super(MainWindow, self).''init''(parent)<br /> self.resize(731, 475)<br /> centralwidget = QWidget(self)<br /> gridLayout = QGridLayout(centralwidget)<br /> # textEdit needs to be a class variable.<br /> self.textEdit = QTextEdit(centralwidget)<br /> gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)<br /> self.setCentralWidget(centralwidget)<br /> menubar = QMenuBar(self)<br /> menubar.setGeometry(QRect(0, 0, 731, 29))<br /> menu_File = QMenu(menubar)<br /> self.setMenuBar(menubar)<br /> statusbar = QStatusBar(self)<br /> self.setStatusBar(statusbar)<br /> actionShow_GPL = QAction(self)<br /> actionShow_GPL.triggered.connect(self.showGPL)<br /> action_About = QAction(self)<br /> action_About.triggered.connect(self.about)<br /> iconToolBar = self.addToolBar(&quot;iconBar.png&amp;quot;)<br />#——————————————————<br /># Add icons to appear in tool bar - step 1<br /> actionShow_GPL.setIcon(QIcon(&quot;:/showgpl.png&amp;quot;))<br /> action_About.setIcon(QIcon(&quot;:/about.png&amp;quot;))<br /> action_Close = QAction(self)<br /> action_Close.setCheckable(False)<br /> action_Close.setObjectName(&quot;action_Close&amp;quot;)<br /> action_Close.setIcon(QIcon(&quot;:/quit.png&amp;quot;))<br />#——————————————————<br /># Show a tip on the Status Bar - step 2<br /> actionShow_GPL.setStatusTip(&quot;Show GPL Licence&amp;quot;)<br /> action_About.setStatusTip(&quot;Pop up the About dialog.&quot;)<br /> action_Close.setStatusTip(&quot;Close the program.&quot;)<br />#——————————————————<br /> menu_File.addAction(actionShow_GPL)<br /> menu_File.addAction(action_About)<br /> menu_File.addAction(action_Close)<br /> menubar.addAction(menu_File.menuAction())
 
iconToolBar.addAction(actionShow_GPL)<br /> iconToolBar.addAction(action_About)<br /> iconToolBar.addAction(action_Close)<br /> action_Close.triggered.connect(self.close)
 
def showGPL(self):<br /> '''Read and display GPL licence.'''<br /> self.textEdit.setText(open('COPYING.txt').read())
 
def about(self):<br /> '''Popup a box with about message.'''<br /> QMessageBox.about(self, &quot;About PyQt, Platform and the like&amp;quot;,<br /> &quot;&quot;&quot;&lt;b&amp;gt; About this program &lt;/b&amp;gt; v %s<br /> &lt;p&amp;gt;Copyright &amp;copy; 2011 Your Name.<br /> All rights reserved in accordance with<br /> GPL v2 or later - NO WARRANTIES!<br /> &lt;p&amp;gt;This application can be used for<br /> displaying OS and platform details.<br /> &lt;p&amp;gt;Python %s - PySide version %s - Qt version %s on s&amp;quot;&quot;&quot;  (''version'', platform.python_version(), PySide.''version'', PySide.QtCore.''version'', platform.system&amp;amp;#40;&amp;#41;))


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 <span class="caps">GUI</span> 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!<br />[[PySide-Newbie-Tutorials|Return to PySideSimplicissimus]]
if ''name'' == '''main''':<br /> app = QApplication(sys.argv)<br /> frame = MainWindow()<br /> frame.show()<br /> sys.exit(app.exec_())


===Categories:===
<code><br />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.


* [[:Category:LanguageBindings|LanguageBindings]]
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!
** [[:Category:LanguageBindings::PySide|PySide]]
* [[:Category:LanguageBindings::PySide::Newbie-Tutorials|Newbie Tutorials]]

Revision as of 14:42, 23 February 2015


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&quot;:

 git clone https://github.com/OldAl/tuts4pyside<code>

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

  1. combine_allin1.py - combination of ShowGPL, About, Close scripts
  2. The purpose of this version of program is to show implementation
  3. 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&quot;)
#——————————————————
# Add icons to appear in tool bar - step 1
actionShow_GPL.setIcon(QIcon(":/showgpl.png&quot;))
action_About.setIcon(QIcon(":/about.png&quot;))
action_Close = QAction(self)
action_Close.setCheckable(False)
action_Close.setObjectName("action_Close&quot;)
action_Close.setIcon(QIcon(":/quit.png&quot;))
#——————————————————
# Show a tip on the Status Bar - step 2
actionShow_GPL.setStatusTip("Show GPL Licence&quot;)
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&quot;,
"""<b&gt; About this program </b&gt; v %s
<p&gt;Copyright &copy; 2011 Your Name.
All rights reserved in accordance with
GPL v2 or later - NO WARRANTIES!
<p&gt;This application can be used for
displaying OS and platform details.
<p&gt;Python %s - PySide version %s - Qt version %s on s&quot;"" (version, platform.python_version(), PySide.version, PySide.QtCore.version, platform.system&amp;#40;&#41;))

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!