Home > Articles > Programming > Python

A Practical Introduction to PyQt's Undo/Redo Framework

Toggle Open Article Table of ContentsContents

Close Table of ContentsContents

  1. A Practical Introduction to PyQts Undo/Redo Framework
  2. Adding Undo/Redo to the Mix
  3. Using the Undo/Redo Framework
  4. Summary
Close Table of Contents
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Every user experiences the occasional "oops." Luckily for most of us, smart programmers anticipate the possibility and provide undo/redo facilities to help us out of such jams. Mark Summerfield shows you how to be that smart sort of Python programmer, with this introduction to PyQt's undo/redo framework.

Creating native and good-looking cross-platform applications is just about as easy and pleasant as it can get when you use PyQt (the Python bindings for the Qt application development framework), especially if you design your forms visually using Qt Designer. Using QActions or direct signal-slot connections, you can invoke any callable you like in response to user interaction, such as when the user clicks a button or chooses a menu option.

Here’s a quick reminder of just how simple it all is:

# simple.pyw
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Form(QDialog):

    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.buttonA = QPushButton("Button &A")
        self.buttonB = QPushButton("Button &B")
        self.label = QLabel("No button has been clicked")
        layout = QVBoxLayout()
        layout.addWidget(self.buttonA)
        layout.addWidget(self.buttonB)
        layout.addWidget(self.label)
        self.setLayout(layout)
        self.connect(self.buttonA, SIGNAL("clicked()"), self.pressedA)
        self.connect(self.buttonB, SIGNAL("clicked()"), self.pressedB)
        self.setWindowTitle("Simple")

    def pressedA(self):
        self.label.setText("Button A clicked")

    def pressedB(self):
        self.label.setText("Button B clicked")

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

This code produces the application shown in Figure 1. I could have used lambda or partial-function application and connected both buttons to a single method, but that isn’t the concern here.

Figure 1

Figure 1 The simple.pyw application.

This application shows one feature that’s common to many PyQt (and C++/Qt) applications: Each action is connected directly to a method that carries out the action. This is easy to program and maintain. As I’ll show shortly, however, you must take a slightly different approach if you want to provide undo/redo facilities.

  • Share ThisShare This
  • Save To Your Account

Discussions

comments powered by Disqus

Related Resources

#TuesdayTrivia: Spotlight on WP7 (Win a copy of Sams Teach Yourself Windows Phone 7 Application Development)
By on May 2, 2012Comments
These days, what CAN'T a smartphone do? Microsoft is putting their own spin on things to help you experience "life in motion" when using your device. Instead of containing static application icons, the re-imagined Start screen features live Tiles showing real-time content updates.

March Trivia #1: Let there be light! (Win Microsoft Visual Studio LightSwitch Unleashed)
By on March 13, 2012Comments
Want a simplified self-service tool to help you build business applications for the desktop and beyond? Microsoft programmers… meet Visual Studio LightSwitch.

February Trivia #2: There's an App for That (Win Sams Teach Yourself iOS 5 Application Development in 24 Hours)
By on February 28, 2012Comments
In less than a decade, the iOS platform has changed the way we think about mobile communication.

See All Related Blogs