Exercise

Write a stand-alone string list editing dialog. The dialog should use if_name_

== "_main_": so that it can be run and tested independently. It should look like the dialog shown in Figure 5.8.

The strings should be held in a QListWidget. The Sort button is easy to implement since we can connect its clicked() signal directly to the QListWid-get.sortItems() method.

The dialog should work on its own string list, either a copy of one passed in, or one it creates itself, and when accepted should emit a signal containing the list (as a QStringList), and also have a publically accessible data attribute, stringlist.

The reject() slot should be implemented like this:

Figure 5.8 The String List dialog with an item being added

def reject(self): self.accept()

For testing purposes put the following code at the end of the file:

if name_

fruit =

main

["Banana", "Apple", "Elderberry", "Clementine", "Fig", "Guava", "Mango", "Honeydew Melon", "Date", "Watermelon", "Tangerine", "Ugli Fruit", "Juniperberry", "Kiwi", "Lemon", "Nectarine", "Plum", "Raspberry", "Strawberry", "Orange"] app = QApplication(sys.argv) form = StringListDlg("Fruit", fruit) form.exec_()

print "\n".join([unicode(x) for x in form.stringlist])

This creates a StringListDlg instance, with a string that names the kind of things in the list, and a list of strings, and then calls it modally. When the user closes the dialog we print the list of strings on the console so that we can see the effects of our edits.

You will need to read the documentation for QListWidget, and for QInputDia-log.getText() which can be used for getting a string to be added and for editing an existing string. This exercise can be done in about 120 lines of code.

A model answer is provided by the file chap05/stringlistdlg.py. The program can be tested by running it. (On Windows, it should be run from a console; on Mac OS X, from a Terminal.)

• Creating a Main Window

• Handling User Actions

Was this article helpful?

0 0
Tuberminator

Tuberminator

The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.

Get My Free Ebook


Post a comment