source: trunk/examples/sql/overview/table4/main.cpp

Last change on this file was 202, checked in by rudi, 14 years ago

Added SQL examples

File size: 2.8 KB
Line 
1/****************************************************************************
2** $Id: main.cpp 2051 2007-02-21 10:04:20Z chehrlic $
3**
4** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include "main.h"
12
13
14StatusPicker::StatusPicker( QWidget *parent, const char *name )
15 : QComboBox( parent, name )
16{
17 QSqlCursor cur( "status" );
18 cur.select( cur.index( "name" ) );
19
20 int i = 0;
21 while ( cur.next() ) {
22 insertItem( cur.value( "name" ).toString(), i );
23 index2id[i] = cur.value( "id" ).toInt();
24 i++;
25 }
26}
27
28
29int StatusPicker::statusId() const
30{
31 return index2id[ currentItem() ];
32}
33
34
35void StatusPicker::setStatusId( int statusid )
36{
37 QMap<int,int>::Iterator it;
38 for ( it = index2id.begin(); it != index2id.end(); ++it ) {
39 if ( it.data() == statusid ) {
40 setCurrentItem( it.key() );
41 break;
42 }
43 }
44}
45
46
47void CustomTable::paintField( QPainter * p, const QSqlField* field,
48 const QRect & cr, bool b)
49{
50 if ( !field )
51 return;
52 if ( field->name() == "statusid" ) {
53 QSqlQuery query( "SELECT name FROM status WHERE id=" +
54 field->value().toString() );
55 QString text;
56 if ( query.next() ) {
57 text = query.value( 0 ).toString();
58 }
59 p->drawText( 2,2, cr.width()-4, cr.height()-4, fieldAlignment( field ), text );
60 }
61 else {
62 QDataTable::paintField( p, field, cr, b) ;
63 }
64}
65
66
67QWidget *CustomSqlEditorFactory::createEditor(
68 QWidget *parent, const QSqlField *field )
69{
70 if ( field->name() == "statusid" ) {
71 QWidget *editor = new StatusPicker( parent );
72 return editor;
73 }
74
75 return QSqlEditorFactory::createEditor( parent, field );
76}
77
78
79int main( int argc, char *argv[] )
80{
81 QApplication app( argc, argv );
82
83 if ( createConnections() ) {
84 QSqlCursor staffCursor( "staff" );
85
86 CustomTable *staffTable = new CustomTable( &staffCursor );
87 QSqlPropertyMap *propMap = new QSqlPropertyMap();
88 CustomSqlEditorFactory *editorFactory = new CustomSqlEditorFactory();
89 propMap->insert( "StatusPicker", "statusid" );
90 staffTable->installPropertyMap( propMap );
91 staffTable->installEditorFactory( editorFactory );
92
93 app.setMainWidget( staffTable );
94
95 staffTable->addColumn( "forename", "Forename" );
96 staffTable->addColumn( "surname", "Surname" );
97 staffTable->addColumn( "salary", "Annual Salary" );
98 staffTable->addColumn( "statusid", "Status" );
99
100 QStringList order = QStringList() << "surname" << "forename";
101 staffTable->setSort( order );
102
103 staffTable->refresh();
104 staffTable->show();
105
106 return app.exec();
107 }
108
109 return 1;
110}
Note: See TracBrowser for help on using the repository browser.