Page List

Thursday, January 26, 2012

Qt 4.8 Application Example - Part 1

This is a walk through of a conversion from C++ to PyQt4 of the Qt Developer Network Application Example.  The original example isn't broken up into parts the way the tutorials were; it is broken up here to make the conversion easier to follow.



#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
    PyQt4 conversion of Qt Application Example
    
    The Application example shows how to implement a standard GUI application 
    with menus, toolbars, and a status bar. The example itself is a simple text 
    editor program built around QPlainTextEdit.
    
    NOTES:
    =====
    This is based on an 'example' vs a tutorial however I'm splitting
    up the steps to make them easier to process.
    
    This first pass handles setting up the application class based on 
    the examples C++ 'include' file: mainwindow.h. Empty slots and methods are 
    defined creating skeleton code. 
    
    The basic logic for building the application is laid out in the '__init__()' 
    method.
    
    Class attributes will be created as the slots/methods are implemented.
    
last modified: 2012-01-25 jg
ref: 
    http://developer.qt.nokia.com/doc/qt-4.8/mainwindows-application.html
    http://developer.qt.nokia.com/doc/qt-4.8/mainwindows-application-mainwindow-h.html
'''
import sys
from PyQt4.QtGui import (QApplication, QMainWindow, QPlainTextEdit)
from PyQt4.QtCore import (pyqtSlot)

class MainWindow(QMainWindow):      # subclass QMainWindow

    def __init__(self, parent=None):    # initiase base class
        super(MainWindow, self).__init__(parent)

        # create GUI
        self._createActions()
        self._createMenus()
        self._createToolBars()
        self._createStatusBar()

        # create central widget        
        self._textEdit = QPlainTextEdit()
        self.setCentralWidget(self._textEdit)

        # 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):
        pass

    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()