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
Tube Traffic Ninja

Tube Traffic Ninja

Discover How You Can Quickly And Easily Dominate Google and YouTube... With Simple Cash Generating Videos. Did you know that YouTube is the second largest search website on the entire Internet? YouTube gets more daily searches than Bing and Yahoo. In fact, there is only one search engine that gets more action.

Get My Free Ebook


Post a comment