Practice Exercises In Python Programming And Qt

Use Qt Designer to create a user interface with one of the designs shown in Figure 7.11, or with a design of your own. You will probably need to use a Grid Layout, as well as Vertical and Horizontal Layouts. For grid layouts, you may have to try a few times, perhaps resizing and positioning widgets to help Qt Designer create the grid you want. Use QDialogButtonBoxes for the buttons.

Ticket Order #1

Ticket Order #1

Figure 7.11 A dialog with two different designs

Figure 7.11 A dialog with two different designs

The price spinbox should have a range of 0.00-5000.00, be right-aligned, and have a prefix of "$ ", as shown in Figure 7.11. The quantity spinbox should have a range of 1-50 and also be right-aligned. Set the date format to be whatever you prefer if you don't like the default.

The widgets you will refer to in code should have sensible names—for example, customerLineEdit and priceSpinBox.

Make the appropriate buddies, that is, from the "customer" Label to its Line Edit, from the "when" Label to the Date Time Edit, and so on. Also make sure that the Tab order is customer, when date, price, quantity, button box.

Create a subclass to use the user interface. The code should ensure that the OK button is enabled only if the customer Line Edit is not empty and the amount is greater than zero. To access a button in a QDialogButtonBox, use the button() method with the button's constant as the argument—for example, buttonBox.button(QDialogButtonBox.Ok).

The amount should be recalculated and shown in the amount label every time the user changes one of the spinbox values. Set the when date's range to be from tomorrow, until next year. Provide a result() method that returns a four-tuple (Unicode, datetime.datetime, float, int) for the customer, when date, price, and quantity. (If you are using a PyQt version prior to 4.1, return the date as a QDateTime; otherwise, use the QDateTime.toPyDateTime() method to get a datetime.datetime.)

Include enough test code at the end to create and show a TicketOrderDlg so that you can interact with it. After the event loop has finished print the tuple returned by the result() method on the console.

The subclass, including the test code, can be written in about 60 lines. If this is the first time you have used Qt Designer it may take 15-20 minutes to get the design right, but with practice a dialog like this should take just a few minutes.

Model solutions are provided in chap07/ticketorderdlg1.ui and chap07/ticket-orderdlg2.ui, with a test program in chap07/ticketorderdlg.py.

This page intentionally left blank

Main Window Responsibilities Data Container Responsibilities Saving and Loading Binary Files Saving and Loading Text Files Saving and Loading XML Files

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