Page List

Monday, January 23, 2012

Qt 4.8 Notepad Tutorial - Part 4

The following code is based on the Qt Getting Started Programming with Qt Hello Notepad tutorial. There are five parts in all. 


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

'''
    PyQt4 conversion of Qt Tutorial 'Hello Notepad'
    
    Many applications will benefit from using a QMainWindow, which has 
    its own layout to which you can add a menu bar, dock widgets, tool bars, 
    and a status bar. QMainWindow has a centre area that can be occupied by 
    any kind of widget. In our case, we will place our text edit there.
    
    NOTES:
    =====
    The 'loadAction' has been renamed '_openAction', 'File->Open...'
    being more of a standard. 
    
    For now, only local variables are used (no attributes)
    The two 'slot' methods have been defined as empty private methods.

last modified: 2012-01-23 jg
ref: http://developer.qt.nokia.com/doc/qt-4.8/gettingstartedqt.html
'''
import sys
from PyQt4.QtGui import (QApplication, QMainWindow, QAction, QTextEdit)
from PyQt4.QtCore import (pyqtSlot)

class Notepad(QMainWindow):

    def __init__(self, parent=None):
        super(Notepad, self).__init__(parent)

        # define menu actions
        openAction = QAction(self.tr("&Open..."), self)
        saveAction = QAction(self.tr("&Save..."), self)
        exitAction = QAction(self.tr("&Exit"), self)

        # set up signal/slot connections for event handling
        openAction.triggered.connect(self._open)
        saveAction.triggered.connect(self._save)
        exitAction.triggered.connect(QApplication.instance().quit)

        # create a dropdown 'File' menu on the window menu bar
        fileMenu = self.menuBar().addMenu(self.tr("&File..."))

        # add the open and save actions to the File menu
        fileMenu.addAction(openAction)
        fileMenu.addAction(saveAction)

        # add the exit action to the menu bar as a separate menu item
        self.menuBar().addAction(exitAction)

        # create the textEdit widget and add it as
        # the central widget for the window
        textEdit = QTextEdit()
        self.setCentralWidget(textEdit)

        self.setWindowTitle(self.tr("Notepad"))

    # define private slots (methods) ------------------------------------------
    @pyqtSlot()
    def _open(self):
        pass

    @pyqtSlot()
    def _save(self):
        pass

def main():
    app = QApplication(sys.argv)

    notepad = Notepad()
    notepad.show()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()