Home > Articles > Open Source > Python

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

A Simple GUI Using PyQt's Data-Aware Objects

If are looking for the easiest way to create a database application, you might want to take a look at PyQt. PyQt, the Python bindings to the cross-platform GUI toolkit Qt, supports the designer files created by Qt Designer. Using Qt Designer, you can easily create forms that connect to any database.

From these forms, you can generate either Python or C++ code. I have written a book on working with PyQt and Designer (GUI Programming with Python: Qt Edition) that deals with this toolkit. Here, I just want to show you the steps you need to create data-aware forms.

When would it be appropriate to use PyQt's database widgets? The following points need consideration:

  • You are prototyping and might want to migrate your design to C++ at a certain point in time.

  • A two-tier architecture suffices. You don't think you will need to provide alternative interfaces, such as a Web interface or a report interface, to your database (since those would require coding the application logic again.)

  • You do not use complex queries that link tables.

  • You want to deploy on UNIX/X11, Windows, or OS X.

You are using PyQt anyway. Using PyQt's SQL classes enables you to eliminate the need for extra database-related modules, making distribution of your application easier.

When you have decided to use Qt's data-aware controls, your life suddenly becomes very simple. In Qt Designer, you first create a new database connection. See Figure 1.

Figure 1Figure 1 Creating a database connection.

When that is done, you can start designing a form. In this case, we have a simple window, of the type QDialog, and one of three data-aware controls will be placed in it. You have a choice between QDataTable, which presents the data in a tabular form; QDataBrowser, which presents one record in a simple form; and QDataView, a read-only form.

Creating the form is made easy by the Data Table Wizard (see Figure 2). First you select the database connection and the table you want to select from.

Figure 2Figure 2 Selecting the database connection and the table.

Then you select the fields that you want to include in the table (see Figure 3).

Figure 3Figure 3 Selecting the fields that should be displayed.

In the next step, you select the way the form will interact with the user when confirmations are needed and whether the user can sort on columns (see Figure 4).

Figure 4Figure 4 User interaction options.

It is possible to create a specific SQL WHERE clause, and to select the columns to use in the ORDER BY clause (see Figure 5).

Figure 5Figure 5 Adding SQL code.

Finally, you can set autoediting on or off. Autoediting kicks in when the changes are made to the contents of the QDataTable; if it's true, changes are automatically committed when the user navigates to the next record. (See Figure 6.)

Figure 6Figure 6 Setting autoediting.

The result is a database-aware table that runs even inside Qt Designer (see Figure 7).

Figure 7Figure 7 Running the form inside Qt Designer.

You can save your design to a .ui designer file and then create Python source code using a simple command-line command:

pyuic -x form1.ui > form1.py

However, the generated form isn't immediately useful. You have to embed it in a script where you create a database connection using QSqlDatabase:

import form1, sys
if __name__ == "__main__":
  a = QApplication(sys.argv)
  db = QSqlDatabase.addDatabase("QMYSQL3")
  if db:
    if not db.open():
      print "Could not open testdb database"
      print db.lastError().driverText()
      print db.lastError().databaseText()
  w = form1.Form1()

When you run this script and the database exists and is running, and when your Qt is compiled with the right drivers, you will be greeted once again by your form filled with data.

Of course, Qt wouldn't be what it is—as complete as Java, yet simple and compact—if it didn't also provide classes that you can use for more complex database handling. These are the QSQl module classes, such as QSqlDatabase, QSqlQuery, and QSqlCursor. Using those, however, means that you are leaving the balmy country of data-aware widgets and that you have to start rolling your own solution again.

Nowadays Python can hold its own in the wonderful world of databases and GUI applications. For simple tasks, you can select PyQt and its data-aware widgets, and be done quickly with good, efficient results. More complex tasks demand a more complex approach, but there is no dearth of powerful tools libraries to choose from.

  • + Share This
  • 🔖 Save To Your Account