#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' PyQt4 conversion of Qt Application Example Before we can implement 'createActions()': 1. save the application icons to an 'images' directory within your source file directory 2. save the 'application.qrc' file in your source directory 3. compile the 'application.qrc' file using the 'pyrcc4' utility that installs with PyQt4: pyrcc4 -py3 -o qrc_app.py application.qrc 4. import the newly created 'qrc_app' file 5. implement 'createActions()' BEHAVIOUR: ========= It looks as if nothing has changed but you want to make sure everything complies ok. NOTES: ===== Had to move the creation of the 'self._textEdit' widget to the beginning of the '__init__' method to make it available during later method calls. last modified: 2012-01-25 jg ref: http://developer.qt.nokia.com/doc/qt-4.8/mainwindows-application.html http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/resources.html http://developer.qt.nokia.com/doc/qt-4.8/mainwindows-application-application-qrc.html ''' import sys from PyQt4.QtGui import (QApplication, QMainWindow, QPlainTextEdit, QAction, QIcon, QKeySequence) from PyQt4.QtCore import (pyqtSlot) import qrc_app class MainWindow(QMainWindow): # subclass QMainWindow def __init__(self, parent=None): # initialise base class super(MainWindow, self).__init__(parent) # create central widget self._textEdit = QPlainTextEdit() self.setCentralWidget(self._textEdit) # create GUI self._createActions() self._createMenus() self._createToolBars() self._createStatusBar() # connect signals/slots for event handling self._textEdit.document().contentsChanged.connect(self._documentWasModified) # establish initial conditions self._readSettings() self._setCurrentFile('') self.setUnifiedTitleAndToolBarOnMac(True) # overridden methods ------------------------------------------------------ def closeEvent(self, evt): pass # private slots ----------------------------------------------------------- @pyqtSlot() def _newFile(self): pass @pyqtSlot() def _open(self): pass @pyqtSlot() def _save(self): pass @pyqtSlot() def _saveAs(self): pass @pyqtSlot() def _about(self): pass @pyqtSlot() def _documentWasModified(self): pass # private methods --------------------------------------------------------- def _createActions(self): self._newAct = QAction(QIcon(":/images/new.png"), self.tr("&New"), self) self._newAct.setShortcuts(QKeySequence.New) self._newAct.setStatusTip((self.tr("Create a new file."))) self._newAct.triggered.connect(self._newFile) self._openAct = QAction(QIcon(":/images/open.png"), self.tr("&Open"), self) self._openAct.setShortcuts(QKeySequence.Open) self._openAct.setStatusTip((self.tr("Open a file."))) self._openAct.triggered.connect(self._open) self._saveAct = QAction(QIcon(":/images/save.png"), self.tr("&Save"), self) self._saveAct.setShortcuts(QKeySequence.Save) self._saveAct.setStatusTip((self.tr("Save the document to disk."))) self._saveAct.triggered.connect(self._save) self._saveAsAct = QAction(self.tr("Save &As..."), self) self._saveAsAct.setShortcuts(QKeySequence.SaveAs) self._saveAsAct.setStatusTip((self.tr("Save the document under a new name."))) self._saveAsAct.triggered.connect(self._saveAs) self._exitAct = QAction(self.tr("E&xit"), self) self._exitAct.setShortcuts(QKeySequence.Quit) self._exitAct.setStatusTip((self.tr("Exit the application."))) self._exitAct.triggered.connect(self.close) self._aboutAct = QAction(self.tr("&About"), self) self._aboutAct.setStatusTip((self.tr("Show the application's About box."))) self._aboutAct.triggered.connect(self._about) self._aboutQtAct = QAction(self.tr("About &Qt"), self) self._aboutQtAct.setStatusTip((self.tr("Show the Qt library's About box."))) self._aboutQtAct.triggered.connect(QApplication.instance().aboutQt) # actions that connect to the 'textEdit' widget self._cutAct = QAction(QIcon(":/images/cut.png"), self.tr("Cu&t"), self) self._cutAct.setShortcuts(QKeySequence.Cut) self._cutAct.setStatusTip((self.tr("Cut the current selection's content to the clipboard."))) self._cutAct.triggered.connect(self._textEdit.cut) self._copyAct = QAction(QIcon(":/images/copy.png"), self.tr("&Copy"), self) self._copyAct.setShortcuts(QKeySequence.Copy) self._copyAct.setStatusTip((self.tr("Copy the current selection's content to the clipboard."))) self._copyAct.triggered.connect(self._textEdit.copy) self._pasteAct = QAction(QIcon(":/images/paste.png"), self.tr("&Paste"), self) self._pasteAct.setShortcuts(QKeySequence.Paste) self._pasteAct.setStatusTip((self.tr("Paste the clipboard contents into the current selection."))) self._pasteAct.triggered.connect(self._textEdit.paste) # set action visibility self._cutAct.setEnabled(False) self._copyAct.setEnabled((False)) self._textEdit.copyAvailable.connect(self._cutAct.setEnabled) self._textEdit.copyAvailable.connect(self._copyAct.setEnabled) def _createMenus(self): pass def _createToolBars(self): pass def _createStatusBar(self): pass def _readSettings(self): pass def _writeSettings(self): pass def _maybeSave(self): pass def _loadFile(self, fname): pass def _saveFile(self, fname): pass def _setCurrentFile(self, fname): pass def _strippedName(self, fullFName): pass # main ======================================================================== def main(): app = QApplication(sys.argv) app.setOrganizationName("My Business") app.setApplicationName("Application Example") mw = MainWindow() mw.setWindowTitle("Application Example") mw.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
Page List
▼
Thursday, January 26, 2012
Qt 4.8 Application Example - Part 2
In this step the application interface doesn't look as if anything has changed; however, the resource icons and application.qrc files have been saved and compiled using the pyrcc4 utility (see comments at the head of the code for step by step instructions) and the createActions() method has been implemented.