designer-manual-7.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 407 行 · 第 1/4 页

HTML
407
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/tools/designer/book/chap-creating-database-applications.leaf:3 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Creating Database Applications</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><p align="right">[<a href="designer-manual-6.html">Prev: Creating Custom Widgets</a>] [<a href="designer-manual.html">Home</a>] [<a href="designer-manual-8.html">Next: Customizing and Integrating Qt Designer</a>]</p><h2 align="center">Creating Database Applications</h2><!-- index Databases --><!-- index SQL --><p>This chapter shows you how to use Qt's data-aware widgets from within <em>Qt Designer</em>. It demonstrates <tt>INSERT</tt>, <tt>UPDATE</tt> and <tt>DELETE</tt> in both <b>QDataTable</b>s (tables) and <b>QDataBrowser</b>s (forms). It also shows how to code Master-Detail relationships and Drilldown. A simple approach to foreign key handling is presented here; a more sophisticated approach is shown in the online SQL module documentation.</p><!-- index Databases!Drivers --><!-- index QODBC3, Database driver --><!-- index QOCI8, Database driver --><!-- index QPSQL7, Database driver --><!-- index QMYSQL3, Database driver --><!-- index Databases!Drivers!QODBC3 --><!-- index Databases!Drivers!QOCI8 --><!-- index Databases!Drivers!QPSQL7 --><!-- index Databases!Drivers!QMYSQL3 --><p>If you wish to run the examples or create your own applications using these widgets you need access to an SQL database and a Qt database driver that can connect to the database. At the time of writing the drivers that Qt supports are QODBC3 (Open Database Connectivity), QOCI8 (Oracle), QPSQL7 (PostgreSQL 6 and 7) and QMYSQL3 (MySQL).</p><!-- index Widgets!Data Aware --><!-- index Data Aware Widgets --><!-- index Databases!Data Aware Widgets --><p>Although you can use the Qt data-aware widgets to browse and edit data in SQL databases without having to write any SQL, a basic understanding of SQL is highly recommended. We assume that you have some familiarity with <tt>SELECT</tt>, <tt>INSERT</tt>, <tt>UPDATE</tt> and <tt>DELETE</tt> statements. We also assume a basic understanding of the concepts of normalisation and of primary and foreign keys. A standard text covering SQL databases is <em>An Introduction to Database Systems (7th ed.)</em> by C. J. Date, ISBN 0201385902.</p><!-- index In-place Editing --><!-- index Databases!In-place Editing --><p>In the following text we describe the creation of a 'book' database application. The application demonstrates how to use <b>QDataTable</b>s including in-place record editing and how to set up master-detail relationships between <b>QDataTable</b>s. It also explains how to drill down from a <b>QDataTable</b> to another widget, for example, to a <b>QDataBrowser</b> or a <b>QDataView</b> and how to perform record editing in a <b>QDataBrowser</b>. A great deal of functionality is available from the classes directly in <em>Qt Designer</em> although subclassing is always available for finer control. If you want to build the 'book' examples you will need to create the example schema on your database.</p><p align="center"><img align="middle" src="book-main.png" width="502" height="532"></p><blockquote><p align="center"><em>The Book Application</em></p></blockquote><blockquote><p align="center"><b> The Example Schema</b></p><p>Note that the examples in this chapter all use the tables, views and records which are defined in the <tt>qt/tools/designer/examples/book/book.sql</tt> file. This file has been tested with PostgreSQL 6 and PostgreSQL 7. You may need to modify the SQL in this file to recreate the example database on your own system.</p><p>Schema <tt>CREATE TABLE</tt> Statements</p><pre>    CREATE TABLE author    ( id integer primary key,    forename varchar(40),    surname varchar(40) );</pre><pre>    CREATE TABLE book    ( id integer primary key,    title varchar(40),    price numeric(10,2),    authorid integer,    notes varchar(255) );</pre><pre>    CREATE TABLE sequence    ( tablename varchar(10),    sequence numeric);</pre> <p>The 'book' table is simplified for the purposes of the example. It can only relate a book to a single author (authorid) and lacks an ISBN field. The 'sequence' table is used for generating unique index values for the example tables. Note that SQL databases often provide their own method for creating sequences (for example, using the <tt>CREATE SEQUENCE</tt> command) which is very likely to be a more optimal solution. For the sake of portability the examples will use a 'sequence' table which will work with the vast majority of SQL databases.</p></blockquote><h3><a name="1"></a>Setting Up Database Connections</h3><!-- index Databases!Connecting to Database Servers --><!-- index Connecting!Databases to Database Servers --><p>There are two aspects of database connections that we must consider. Firstly the connection we wish to use within <em>Qt Designer</em> itself, and secondly the connection we wish to use in the applications that we create.</p><h4><a name="1-1"></a>Setting Up Qt Designer's Connections</h4><p align="center"><img align="middle" src="database-connections.png" width="532" height="341"></p><blockquote><p align="center"><em>Database Connections Dialog</em></p></blockquote><p>Choose <b>Project|Database Connections</b> from the menu bar. The <em>Database Connections</em> dialog will appear. Click <b>New Connection</b>. For applications that use a single database it will probably be most convenient to use the default connection name of '(default)'. If you use more than one database then each one must be given a unique name. A driver must be chosen from the Driver combo box. The database name may be available in the Database Name combo box or may have to be typed in. The database name, username, password and hostname should be provided by your database system administrator. When the Connection information has been completed click <b>Connect</b>. If the connection is made the connection name will appear in the list box on the left hand side of the dialog. You can now close the dialog; the connection settings will remain in effect until you change or delete them or exit from <em>Qt Designer</em>.</p><!-- index Projects!Database Connections --><p><em>Qt Designer</em> can remember database connection settings in <tt>qmake</tt> project files. Create a new project, e.g. click <b>File|New</b>, then click the 'C++ Project' icon to invoke the <em>Project Settings</em> dialog. Click the ellipsis button to invoke the <em>Save As</em> dialog; navigate to the project's directory (creating it if necessary). Make sure you're in the project's directory, then enter a project name of 'book.pro'. Click the <b>Save</b> button to return to the <em>Project Settings</em> dialog, then click <b>OK</b>. Next time you start <em>Qt Designer</em> instead of opening individual <tt>.ui</tt> files open the <tt>.pro</tt> project file instead and <em>Qt Designer</em> will automatically reload the project's connection settings. To activate the connection click <b>Project|Database Connections</b>. The connections previously saved with the project will be listed in the left hand list box. Click the connection you wish to use and then click <b>Connect</b>. This connection will be used from now on, e.g. for previewing <b>QDataTable</b>s. Opening a project file also causes <em>Qt Designer</em> to load in the list of forms associated with the project into the Form List window. In most of the explanation that follows we will assume that you use project files and have clicked <b>Connect</b> so that there is always a connection available when you work in <em>Qt Designer</em>.</p><h4><a name="1-2"></a>Setting Up Connections for Applications</h4><p>The applications you create must make their own connections to the SQL database. We provide an example function, <tt>createConnections()</tt>, that you can use as a basis for your own code.<!-- index createConnections() --></p><pre>bool createConnections(){    // create the default database connection    QSqlDatabase *defaultDB = QSqlDatabase::addDatabase( "QPSQL7" );    if ( ! defaultDB ) {        qWarning( "Failed to connect to driver" );        return FALSE;    }    defaultDB-&gt;setDatabaseName( "book" );    defaultDB-&gt;setUserName( "bookuser" );    defaultDB-&gt;setPassword( "bookpw" );    defaultDB-&gt;setHostName( "bookhost" );    if ( ! defaultDB-&gt;open() ) {        qWarning( "Failed to open books database: " +                  defaultDB-&gt;lastError().driverText() );        qWarning( defaultDB-&gt;lastError().databaseText() );        return FALSE;    }    return TRUE;}</pre><!-- index addDatabase() --><p>We call <tt>addDatabase()</tt> passing it the name of the driver we wish to use. We then set the connection information by calling the <tt>set</tt>... functions. Finally we attempt to open the connection. If we succeed we return TRUE, otherwise we output some error information and return FALSE. From <tt>qt/tools/designer/examples/book/book1/main.cpp</tt></p><pre>    int main( int argc, char *argv[] )    {        <a href="qapplication.html">QApplication</a> app( argc, argv );        if ( ! createConnections() )            return 1;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?