1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/sql.doc:54 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>sql/overview/table4/main.cpp Example File</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>sql/overview/table4/main.cpp Example File</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <pre>/****************************************************************************
|
---|
36 | ** $Id: sql-overview-table4-main-cpp.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
37 | **
|
---|
38 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
39 | **
|
---|
40 | ** This file is part of an example program for Qt. This example
|
---|
41 | ** program may be used, distributed and modified without limitation.
|
---|
42 | **
|
---|
43 | *****************************************************************************/
|
---|
44 |
|
---|
45 | #include "main.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | <a name="f12"></a>StatusPicker::StatusPicker( <a href="qwidget.html">QWidget</a> *parent, const char *name )
|
---|
49 | : <a href="qcombobox.html">QComboBox</a>( parent, name )
|
---|
50 | {
|
---|
51 | <a href="qsqlcursor.html">QSqlCursor</a> cur( "status" );
|
---|
52 | cur.<a href="qsqlcursor.html#select">select</a>( cur.<a href="qsqlcursor.html#index">index</a>( "name" ) );
|
---|
53 |
|
---|
54 | int i = 0;
|
---|
55 | while ( cur.<a href="qsqlquery.html#next">next</a>() ) {
|
---|
56 | <a href="qcombobox.html#insertItem">insertItem</a>( cur.<a href="qsqlquery.html#value">value</a>( "name" ).toString(), i );
|
---|
57 | index2id[i] = cur.<a href="qsqlquery.html#value">value</a>( "id" ).toInt();
|
---|
58 | i++;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | int <a name="f13"></a>StatusPicker::statusId() const
|
---|
64 | {
|
---|
65 | return index2id[ currentItem() ];
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | void <a name="f14"></a>StatusPicker::setStatusId( int statusid )
|
---|
70 | {
|
---|
71 | QMap<int,int>::Iterator it;
|
---|
72 | for ( it = index2id.begin(); it != index2id.end(); ++it ) {
|
---|
73 | if ( it.data() == statusid ) {
|
---|
74 | setCurrentItem( it.key() );
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | void CustomTable::<a href="qdatatable.html#paintField">paintField</a>( <a href="qpainter.html">QPainter</a> * p, const <a href="qsqlfield.html">QSqlField</a>* field,
|
---|
82 | const <a href="qrect.html">QRect</a> & cr, bool b)
|
---|
83 | {
|
---|
84 | if ( !field )
|
---|
85 | return;
|
---|
86 | if ( field-><a href="qsqlfield.html#name">name</a>() == "statusid" ) {
|
---|
87 | <a href="qsqlquery.html">QSqlQuery</a> query( "SELECT name FROM status WHERE id=" +
|
---|
88 | field-><a href="qsqlfield.html#value">value</a>().toString() );
|
---|
89 | <a href="qstring.html">QString</a> text;
|
---|
90 | if ( query.<a href="qsqlquery.html#next">next</a>() ) {
|
---|
91 | text = query.<a href="qsqlquery.html#value">value</a>( 0 ).toString();
|
---|
92 | }
|
---|
93 | p-><a href="qpainter.html#drawText">drawText</a>( 2,2, cr.<a href="qrect.html#width">width</a>()-4, cr.<a href="qrect.html#height">height</a>()-4, fieldAlignment( field ), text );
|
---|
94 | }
|
---|
95 | else {
|
---|
96 | QDataTable::<a href="qdatatable.html#paintField">paintField</a>( p, field, cr, b) ;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | QWidget *CustomSqlEditorFactory::<a href="qsqleditorfactory.html#createEditor">createEditor</a>(
|
---|
102 | <a href="qwidget.html">QWidget</a> *parent, const <a href="qsqlfield.html">QSqlField</a> *field )
|
---|
103 | {
|
---|
104 | if ( field-><a href="qsqlfield.html#name">name</a>() == "statusid" ) {
|
---|
105 | <a href="qwidget.html">QWidget</a> *editor = new StatusPicker( parent );
|
---|
106 | return editor;
|
---|
107 | }
|
---|
108 |
|
---|
109 | return QSqlEditorFactory::createEditor( parent, field );
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | int main( int argc, char *argv[] )
|
---|
114 | {
|
---|
115 | <a href="qapplication.html">QApplication</a> app( argc, argv );
|
---|
116 |
|
---|
117 | if ( createConnections() ) {
|
---|
118 | <a href="qsqlcursor.html">QSqlCursor</a> staffCursor( "staff" );
|
---|
119 |
|
---|
120 | CustomTable *staffTable = new CustomTable( &staffCursor );
|
---|
121 | <a href="qsqlpropertymap.html">QSqlPropertyMap</a> *propMap = new <a href="qsqlpropertymap.html">QSqlPropertyMap</a>();
|
---|
122 | CustomSqlEditorFactory *editorFactory = new CustomSqlEditorFactory();
|
---|
123 | propMap-><a href="qsqlpropertymap.html#insert">insert</a>( "StatusPicker", "statusid" );
|
---|
124 | staffTable-><a href="qdatatable.html#installPropertyMap">installPropertyMap</a>( propMap );
|
---|
125 | staffTable-><a href="qdatatable.html#installEditorFactory">installEditorFactory</a>( editorFactory );
|
---|
126 |
|
---|
127 | app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( staffTable );
|
---|
128 |
|
---|
129 | staffTable-><a href="qdatatable.html#addColumn">addColumn</a>( "forename", "Forename" );
|
---|
130 | staffTable-><a href="qdatatable.html#addColumn">addColumn</a>( "surname", "Surname" );
|
---|
131 | staffTable-><a href="qdatatable.html#addColumn">addColumn</a>( "salary", "Annual Salary" );
|
---|
132 | staffTable-><a href="qdatatable.html#addColumn">addColumn</a>( "statusid", "Status" );
|
---|
133 |
|
---|
134 | <a href="qstringlist.html">QStringList</a> order = QStringList() << "surname" << "forename";
|
---|
135 | staffTable-><a href="qdatatable.html#setSort">setSort</a>( order );
|
---|
136 |
|
---|
137 | staffTable-><a href="qdatatable.html#refresh">refresh</a>();
|
---|
138 | staffTable-><a href="qwidget.html#show">show</a>();
|
---|
139 |
|
---|
140 | return app.<a href="qapplication.html#exec">exec</a>();
|
---|
141 | }
|
---|
142 |
|
---|
143 | return 1;
|
---|
144 | }
|
---|
145 | </pre><!-- eof -->
|
---|
146 | <p><address><hr><div align=center>
|
---|
147 | <table width=100% cellspacing=0 border=0><tr>
|
---|
148 | <td>Copyright © 2007
|
---|
149 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
150 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
151 | </table></div></address></body>
|
---|
152 | </html>
|
---|