PySideSimplicissimus Module 2 CloseButton: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Change category "LanguageBindings" -> "PySide")
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
* '''Note:''' this article is a member of the multipart [[PySide-Newbie-Tutorials|PySide_Newbie_Tutorials]]
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}


'''English''' [http://qt-devnet.developpez.com/tutoriels/pyside/simplissimus/1-fermer/ French] ''[qt-devnet.developpez.com]'' [[PySideSimplicissimus Module 2 CloseButton Japanese|日本語]]
[[Category:PySide]]
[[Category:PySide]]


=Close=
* '''Note:''' this article is a member of the multipart [[PySide-Newbie-Tutorials | PySide_Newbie_Tutorials]]


This will not take you more than 5 minutes: the purpose of this program is to enable a click on the '''pushButton''' to close the execution of this program. As always when editing .ui files, we start in '''the Qt Designer'''. Read the [http://doc.qt.io/qt-5/designer-to-know.html getting to know Qt Designer document] ''[doc.qt.io]'' now if you are unfamiliar with Qt Designer. We call the program rather colourfully, '''quitter''', so that our '''Designer''' created MainWindow with one button is saved as '''quitter.ui'''.
'''English''' [[http://qt-devnet.developpez.com/tutoriels/pyside/simplissimus/1-fermer/ French]] [[PySideSimplicissimus_Module_2_CloseButton_Japanese|日本語]]


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:<br /> If you do not have git, pllease install it first. To convert quitter.ui to a python readable format, '''ui_quitter.py''', do the following<br /> The file naming is convenient as one needs to remember only one name: '''quitter'''. The rest of the name helps recognise the purpose of the file.
= Close =


Note: If you don’t have setuptools installed, you will get an error when trying to execute pyside-uic<br />
This will not take you more than 5 minutes: the purpose of this program is to enable a click on the '''pushButton''' to close the execution of this program. As always when editing .ui files, we start in '''the Qt Designer'''. Read the [http://doc.qt.io/qt-5/designer-to-know.html getting to know Qt Designer document] now if you are unfamiliar with Qt Designer. We call the program rather colourfully, '''quitter''', so that our '''Designer''' created MainWindow with one button is saved as '''quitter.ui'''.


You can find setuptools here: http://pypi.python.org/pypi/setuptools ''[pypi.python.org]''
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:
<code> git clone https://github.com/OldAl/tuts4pyside</code>
If you do not have git, pllease install it first. To convert quitter.ui to a python readable format, '''ui_quitter.py''', do the following
<code> pyside-uic quitter.ui -o ui_quitter.py </code>
The file naming is convenient as one needs to remember only one name: '''quitter'''. The rest of the name helps recognise the purpose of the file.
 
Note: If you don't have setuptools installed, you will get an error when trying to execute pyside-uic
<code> Traceback (most recent call last):
File "c:26\Scripts\pyside-uic-script.py", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources</code>
 
You can find setuptools here: [http://pypi.python.org/pypi/setuptools http://pypi.python.org/pypi/setuptools]


The listing of the program follows:
The listing of the program follows:


If you run the program, you will find that it can be closed either by the usual click on the [x], or click of the button. An image of the <span class="caps">GUI</span> can be seen if you click on the following link:
<code>
#!/usr/bin/env python
# quitter.py- provide a button to quit this "program"


import sys
from PySide.QtGui import QMainWindow, QPushButton, QApplication
from ui_quitter import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
if __name__ == '__main__':
    app = QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    app.exec_()
</code>
If you run the program, you will find that it can be closed either by the usual click on the [x], or click of the button. An image of the GUI can be seen if you click on the following link:
* http://akabaila.pcug.org.au/pyside-images/quit.png
* http://akabaila.pcug.org.au/pyside-images/quit.png


Now look closely at the code. Where is the code enabling the button to close the program? It surely is not in the listing…
Now look closely at the code. Where is the code enabling the button to close the program? It surely is not in the listing…


The reason for its absence is that it has been created on the '''Qt Designer''', which has a '''signal-slot''' editor. We used it surreptitiously to connect PushButton.click() “signal” to MainWindow.close() “slot”. It is imbedded in the quitter.ui file and following the conversion, in the ui_quitter.py file. And from that file, we imported Ui_MainWindow class, which is one of the ancestors of our MainWindow class. We use Python’s '''multiple inheritance''' to facilitate use of methods in the ui_quitter.py module.
The reason for its absence is that it has been created on the '''Qt Designer''', which has a '''signal-slot''' editor. We used it surreptitiously to connect PushButton.click() "signal" to MainWindow.close() "slot". It is imbedded in the quitter.ui file and following the conversion, in the ui_quitter.py file. And from that file, we imported Ui_MainWindow class, which is one of the ancestors of our MainWindow class. We use Python's '''multiple inheritance''' to facilitate use of methods in the ui_quitter.py module.


There are other ways to “skin the cat” and we could avoid dual inheritance, but why not use it when it is convenient? The '''super''' digs deeper into the ancestry and initialises the immediate ancestor of '''MainWindow''' and thus the whole tree of ancestry.
There are other ways to "skin the cat" and we could avoid dual inheritance, but why not use it when it is convenient? The '''super''' digs deeper into the ancestry and initialises the immediate ancestor of '''MainWindow''' and thus the whole tree of ancestry.


You will notice that the ''init'' method of the '''MainWindow''' specifies the ancestor as '''None'''. That is kind of indication to PySide that the '''MainWindow''' is indeed the '''Main Window''' of the program.
You will notice that the ''init'' method of the '''MainWindow''' specifies the ancestor as '''None'''. That is kind of indication to PySide that the '''MainWindow''' is indeed the '''Main Window''' of the program.


[[PySide-Newbie-Tutorials|Return to PySideSimplicissimus]]
[[PySide-Newbie-Tutorials | Return to PySideSimplicissimus]]
 
===Categories:===
 
* [[:Category:LanguageBindings|LanguageBindings]]
** [[:Category:LanguageBindings::PySide|PySide]]
* [[:Category:LanguageBindings::PySide::Newbie-Tutorials|Newbie Tutorials]]

Latest revision as of 05:15, 5 June 2016

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 [French] 日本語

Close

This will not take you more than 5 minutes: the purpose of this program is to enable a click on the pushButton to close the execution of this program. As always when editing .ui files, we start in the Qt Designer. Read the getting to know Qt Designer document now if you are unfamiliar with Qt Designer. We call the program rather colourfully, quitter, so that our Designer created MainWindow with one button is saved as quitter.ui.

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:

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

If you do not have git, pllease install it first. To convert quitter.ui to a python readable format, ui_quitter.py, do the following

 pyside-uic quitter.ui -o ui_quitter.py

The file naming is convenient as one needs to remember only one name: quitter. The rest of the name helps recognise the purpose of the file.

Note: If you don't have setuptools installed, you will get an error when trying to execute pyside-uic

 Traceback (most recent call last):
 File "c:26\Scripts\pyside-uic-script.py", line 5, in <module>
 from pkg_resources import load_entry_point
 ImportError: No module named pkg_resources

You can find setuptools here: http://pypi.python.org/pypi/setuptools

The listing of the program follows:

#!/usr/bin/env python
# quitter.py- provide a button to quit this "program"

import sys

from PySide.QtGui import QMainWindow, QPushButton, QApplication

from ui_quitter import Ui_MainWindow

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    app.exec_()

If you run the program, you will find that it can be closed either by the usual click on the [x], or click of the button. An image of the GUI can be seen if you click on the following link:

  • quit.png

Now look closely at the code. Where is the code enabling the button to close the program? It surely is not in the listing…

The reason for its absence is that it has been created on the Qt Designer, which has a signal-slot editor. We used it surreptitiously to connect PushButton.click() "signal" to MainWindow.close() "slot". It is imbedded in the quitter.ui file and following the conversion, in the ui_quitter.py file. And from that file, we imported Ui_MainWindow class, which is one of the ancestors of our MainWindow class. We use Python's multiple inheritance to facilitate use of methods in the ui_quitter.py module.

There are other ways to "skin the cat" and we could avoid dual inheritance, but why not use it when it is convenient? The super digs deeper into the ancestry and initialises the immediate ancestor of MainWindow and thus the whole tree of ancestry.

You will notice that the init method of the MainWindow specifies the ancestor as None. That is kind of indication to PySide that the MainWindow is indeed the Main Window of the program.

Return to PySideSimplicissimus