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

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

Added SQL examples

File size: 2.4 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
14InvoiceItemCursor::InvoiceItemCursor() :
15 QSqlCursor( "invoiceitem" )
16{
17 QSqlFieldInfo productName( "productname", QVariant::String );
18 append( productName );
19 setCalculated( productName.name(), TRUE );
20
21 QSqlFieldInfo productPrice( "price", QVariant::Double );
22 append( productPrice );
23 setCalculated( productPrice.name(), TRUE );
24
25 QSqlFieldInfo productCost( "cost", QVariant::Double );
26 append( productCost );
27 setCalculated( productCost.name(), TRUE );
28}
29
30
31QVariant InvoiceItemCursor::calculateField( const QString & name )
32{
33
34 if ( name == "productname" ) {
35 QSqlQuery query( "SELECT name FROM prices WHERE id=" +
36 field( "pricesid" )->value().toString() );
37 if ( query.next() )
38 return query.value( 0 );
39 }
40 else if ( name == "price" ) {
41 QSqlQuery query( "SELECT price FROM prices WHERE id=" +
42 field( "pricesid" )->value().toString() );
43 if ( query.next() )
44 return query.value( 0 );
45 }
46 else if ( name == "cost" ) {
47 QSqlQuery query( "SELECT price FROM prices WHERE id=" +
48 field( "pricesid" )->value().toString() );
49 if ( query.next() )
50 return QVariant( query.value( 0 ).toDouble() *
51 value( "quantity").toDouble() );
52 }
53
54 return QVariant( QString::null );
55}
56
57
58int main( int argc, char *argv[] )
59{
60 QApplication app( argc, argv );
61
62 if ( createConnections() ) {
63 InvoiceItemCursor invoiceItemCursor;
64
65 QDataTable *invoiceItemTable = new QDataTable( &invoiceItemCursor );
66
67 app.setMainWidget( invoiceItemTable );
68
69 invoiceItemTable->addColumn( "productname", "Product" );
70 invoiceItemTable->addColumn( "price", "Price" );
71 invoiceItemTable->addColumn( "quantity", "Quantity" );
72 invoiceItemTable->addColumn( "cost", "Cost" );
73 invoiceItemTable->addColumn( "paiddate", "Paid" );
74
75 invoiceItemTable->refresh();
76 invoiceItemTable->show();
77
78 return app.exec();
79 }
80
81 return 1;
82}
Note: See TracBrowser for help on using the repository browser.