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

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

Added SQL examples

File size: 2.3 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#include <qdatatable.h>
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
47
48QWidget *CustomSqlEditorFactory::createEditor(
49 QWidget *parent, const QSqlField *field )
50{
51 if ( field->name() == "statusid" ) {
52 QWidget *editor = new StatusPicker( parent );
53 return editor;
54 }
55
56 return QSqlEditorFactory::createEditor( parent, field );
57}
58
59
60int main( int argc, char *argv[] )
61{
62 QApplication app( argc, argv );
63
64 if ( createConnections() ) {
65 QSqlCursor staffCursor( "staff" );
66
67 QDataTable *staffTable = new QDataTable( &staffCursor );
68 QSqlPropertyMap *propMap = new QSqlPropertyMap();
69 CustomSqlEditorFactory *editorFactory = new CustomSqlEditorFactory();
70 propMap->insert( "StatusPicker", "statusid" );
71 staffTable->installPropertyMap( propMap );
72 staffTable->installEditorFactory( editorFactory );
73
74 app.setMainWidget( staffTable );
75
76 staffTable->addColumn( "forename", "Forename" );
77 staffTable->addColumn( "surname", "Surname" );
78 staffTable->addColumn( "salary", "Annual Salary" );
79 staffTable->addColumn( "statusid", "Status" );
80
81 QStringList order = QStringList() << "surname" << "forename";
82 staffTable->setSort( order );
83
84 staffTable->refresh();
85 staffTable->show();
86
87 return app.exec();
88 }
89
90 return 1;
91}
Note: See TracBrowser for help on using the repository browser.