#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' PyQt4 conversion of Qt Application Example Implement createMenus(), createToolBars(), createStatusBar() BEHAVIOUR: ========= The menus and toolbar icons are now available. Move the mouse over them to see their tooltips displayed in the window status bar. NOTES: ===== The menus and toolbar share the QAction objects created earlier. last modified: 2012-01-25 jg ref: http://developer.qt.nokia.com/doc/qt-4.8/mainwindows-application.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): fileMenu = self.menuBar().addMenu(self.tr("&File")) fileMenu.addAction(self._newAct) fileMenu.addAction(self._openAct) fileMenu.addAction(self._saveAct) fileMenu.addAction(self._saveAsAct) fileMenu.addSeparator() fileMenu.addAction(self._exitAct) editMenu = self.menuBar().addMenu(self.tr("&Edit")) editMenu.addAction(self._cutAct) editMenu.addAction(self._copyAct) editMenu.addAction(self._pasteAct) self.menuBar().addSeparator() helpMenu = self.menuBar().addMenu(self.tr("&Help")) helpMenu.addAction(self._aboutAct) helpMenu.addAction(self._aboutQtAct) def _createToolBars(self): fileToolBar = self.addToolBar(self.tr("File")) fileToolBar.addAction(self._newAct) fileToolBar.addAction(self._openAct) fileToolBar.addAction(self._saveAct) editToolBar = self.addToolBar(self.tr("Edit")) editToolBar.addAction(self._cutAct) editToolBar.addAction(self._copyAct) editToolBar.addAction(self._pasteAct) def _createStatusBar(self): self.statusBar().showMessage(self.tr("Ready")) 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()
Thursday, January 26, 2012
Qt 4.8 Application Example - Part 3
In the following code, the createMenus(), createToolBars() and createStatusBar() methods are implemented, using the actions created in Part 2.
Labels:
Qt Application Example