Page List

Monday, January 23, 2012

Qt 4.8 Notepad Tutorial - Part 2

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'
    
    In a real application, you will normally need more than one widget. 
    We will now introduce a QPushButton beneath the text edit. 
    The button will exit the Notepad application when pushed 
    (i.e., clicked on with the mouse).
    
    NOTES:
    =====
    The Quit button uses Qt's signal/slot mechanism to connect an
    event message (signal) with a method (slot). In this example,
    when the Quit button is 'clicked' the QApplication's instance 
    quit() method (slot) is called and the application is closed.
    
    On Windows, the Quit button will not display a line under the 'Q' for
    the short-cut key until the ALT key is pressed.

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, QTextEdit, QPushButton, QWidget,
                         QVBoxLayout)

def main():
    app = QApplication(sys.argv)    # required for all GUI applications

    te = QTextEdit()
    te.setText("Click the Quit button to quit.")

    quitBtn = QPushButton("&Quit")  # ampersand identifies short-cut ALT+Q

    # connect the 'quitBtn' signal 'clicked' with slot 'app.quit'
    quitBtn.clicked.connect(app.quit)

    layout = QVBoxLayout()      # create a vertical box layout
    layout.addWidget(te)        # add the textedit widget
    layout.addWidget(quitBtn)   # add the quit button

    window = QWidget()          # create an empty widget
    window.setLayout(layout)    # give it the layout we just created
    window.show()               # make everything visible

    sys.exit(app.exec_())       # start the main event thread

if __name__ == '__main__':
    main()