designer-manual-7.html

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

HTML
407
字号
<li><p>All that remains is to write the underlying code. All the code snippets are taken from <tt>qt/tools/designer/examples/book/book7/editbook.ui</tt>.</p><ol type=1><li><p>We start with the<!-- index init() --> <tt>init()</tt> function; this is called after the dialog is constructed and we will use it to populate the <em>ComboBox</em> with author names.</p><pre>    void EditBookForm::init()    {        <a href="qsqlquery.html">QSqlQuery</a> query( "SELECT surname FROM author ORDER BY surname;" );        while ( query.<a href="qsqlquery.html#next">next</a>() )            ComboBoxAuthor-&gt;insertItem( query.<a href="qsqlquery.html#value">value</a>( 0 ).toString());    }</pre> <p>Here we execute a query to get a list of author names and insert each one into the <em>ComboBox</em>.</p><li><p>We next write the code which will be executed just before a record is updated (or inserted) in the database.</p><pre>    void EditBookForm::beforeUpdateBook( <a href="qsqlrecord.html">QSqlRecord</a> * buffer )    {        <a href="qsqlquery.html">QSqlQuery</a> query( "SELECT id FROM author WHERE surname ='" +            ComboBoxAuthor-&gt;currentText() + "';" );        if ( query.<a href="qsqlquery.html#next">next</a>() )            buffer-&gt;<a href="qsqlrecord.html#setValue">setValue</a>( "authorid", query.<a href="qsqlquery.html#value">value</a>( 0 ) );    }</pre> <p>We look up the id of the <em>ComboBox</em>'s current author and place it in the update (or insert) buffer's authorid field.</p><li><p>As the user navigates through the records we ensure that the <em>ComboBox</em> reflects the current author.</p><pre>    void EditBookForm::primeUpdateBook( <a href="qsqlrecord.html">QSqlRecord</a> * buffer )    {        // Who is this book's author?        <a href="qsqlquery.html">QSqlQuery</a> query( "SELECT surname FROM author WHERE id='" +            buffer-&gt;<a href="qsqlrecord.html#value">value</a>( "authorid" ).toString() + "';" );        <a href="qstring.html">QString</a> author = "";        if ( query.<a href="qsqlquery.html#next">next</a>() )            author = query.<a href="qsqlquery.html#value">value</a>( 0 ).toString();        // Set the ComboBox to the right author        for ( int i = 0; i &lt; ComboBoxAuthor-&gt;count(); i++ ) {            if ( ComboBoxAuthor-&gt;text( i ) == author ) {                ComboBoxAuthor-&gt;setCurrentItem( i ) ;                break;            }        }    }</pre> <p>Firstly we look up the book's author and secondly we iterate through the <em>ComboBox</em>'s items until we find the author and set the <em>ComboBox</em>'s current item to the matching author.</p></ol></ol><p>If the author name has changed or been deleted the query will fail and no author id will be inserted into the buffer causing the <tt>INSERT</tt> to fail. An alternative is to record the author id's as we populate the <em>ComboBox</em> and store them in a <b>QMap</b> which we can then look up as required. This approach requires changes to the<!-- index init() --> <tt>init()</tt>, <tt>beforeUpdateBook()</tt> and <tt>primeInsertBook()</tt> functions and the addition of a new function, <tt>mapAuthor()</tt>. The relevant code from <tt>qt/tools/designer/examples/book/book8/editbook.ui</tt> is shown below.</p><ol type=1><li><p>First we need to create a class variable to map author names to author id's. Click in the Source tab of the Object Hierarchy, then right click the Class Variables item and click <b>New</b>. Type in 'QMap&lt;QString,int&gt; authorMap;'.</p><li><p>We now record the author id's in the<!-- index init() --> <tt>init()</tt> function.</p><pre>    void EditBookForm::init()    {        <a href="qsqlquery.html">QSqlQuery</a> query( "SELECT surname, id FROM author ORDER BY surname;" );        while ( query.<a href="qsqlquery.html#next">next</a>() ) {            ComboBoxAuthor-&gt;insertItem( query.<a href="qsqlquery.html#value">value</a>( 0 ).toString() );            int id = query.<a href="qsqlquery.html#value">value</a>( 1 ).toInt();            mapAuthor( query.<a href="qsqlquery.html#value">value</a>( 0 ).toString(), id, TRUE );        }    }</pre> <p>After inserting each author's name into the <em>ComboBox</em> we populate a <b>QMap</b> with the author's name and id.</p><li><p>Instead of looking up the author's id in the database we look it up in the <b>QMap</b>.</p><pre>    void EditBookForm::beforeUpdateBook( <a href="qsqlrecord.html">QSqlRecord</a> * buffer )    {        int id;        mapAuthor( ComboBoxAuthor-&gt;currentText(), id, FALSE );        buffer-&gt;<a href="qsqlrecord.html#setValue">setValue</a>( "authorid", id );    }</pre><li> <p>We use a single function for storing author id's and returning them so that we can use a static data structure.</p><pre>    void EditBookForm::mapAuthor( const <a href="qstring.html">QString</a> &amp; name, int &amp; id, bool populate )    {        if ( populate )            authorMap[ name ] = id;        else            id = authorMap[ name ];    }</pre> <p>If the populate flag is TRUE, we store the author's name and id in the <b>QMap</b>, otherwise we look up the given author name and set id appropriately.</p><li><p>Before we perform an update we need to know who the book's author is, and we need to update the combobox.</p><pre>    void EditBookForm::primeUpdateBook( <a href="qsqlrecord.html">QSqlRecord</a> * buffer )    {        // Who is this book's author?        <a href="qsqlquery.html">QSqlQuery</a> query( "SELECT surname FROM author WHERE id=" +            buffer-&gt;<a href="qsqlrecord.html#value">value</a>( "authorid" ).toString() + ";" );        <a href="qstring.html">QString</a> author = "";        if ( query.<a href="qsqlquery.html#next">next</a>() )            author = query.<a href="qsqlquery.html#value">value</a>( 0 ).toString();        // Set the ComboBox to the right author        for ( int i = 0; i &lt; ComboBoxAuthor-&gt;count(); i++ ) {            if ( ComboBoxAuthor-&gt;text( i ) == author ) {                ComboBoxAuthor-&gt;setCurrentItem( i ) ;                break;            }        }    }</pre></ol><!-- index Databases!Foreign Keys --><!-- index Foreign Keys --> <p>Another approach which is especially useful if the same foreign key lookups are required in different parts of the application is to subclass a cursor and use this for our lookups. This is described in the <a href="http://doc.trolltech.com/sql.html">Qt SQL Module documentation</a>, particulary the section on subclassing <b>QSqlCursor</b>.</p><p>The 'book' example demonstrates the basic techniques needed for SQL programming with Qt. Additional information on the Qt SQL classes, especially the <b>QSqlQuery</b> and <b>QSqlCursor</b> classes is provided in the <a href="http://doc.trolltech.com/sql.html">Qt SQL Module documentation</a>.</p><!-- eof --><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><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright &copy; 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>

⌨️ 快捷键说明

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