diff options
author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-07-10 15:24:15 -0500 |
---|---|---|
committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-07-10 15:24:15 -0500 |
commit | bd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch) | |
tree | 7a520322212d48ebcb9fbe1087e7fca28b76185c /examples/sql/overview | |
download | qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip |
Add Qt3 development HEAD version
Diffstat (limited to 'examples/sql/overview')
79 files changed, 1968 insertions, 0 deletions
diff --git a/examples/sql/overview/basicbrowsing/README b/examples/sql/overview/basicbrowsing/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/basicbrowsing/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/basicbrowsing/basicbrowsing.pro b/examples/sql/overview/basicbrowsing/basicbrowsing.pro new file mode 100644 index 0000000..b7245c9 --- /dev/null +++ b/examples/sql/overview/basicbrowsing/basicbrowsing.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp + diff --git a/examples/sql/overview/basicbrowsing/main.cpp b/examples/sql/overview/basicbrowsing/main.cpp new file mode 100644 index 0000000..ae22243 --- /dev/null +++ b/examples/sql/overview/basicbrowsing/main.cpp @@ -0,0 +1,35 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlDatabase *oracledb = QSqlDatabase::database( "ORACLE" ); + // Copy data from the oracle database to the ODBC (default) + // database + QSqlQuery target; + QSqlQuery query( "SELECT id, name FROM people", oracledb ); + if ( query.isActive() ) { + while ( query.next() ) { + target.exec( "INSERT INTO people ( id, name ) VALUES ( " + + query.value(0).toString() + + ", '" + query.value(1).toString() + "' )" ); + } + } + } + + return 0; +} diff --git a/examples/sql/overview/basicbrowsing2/README b/examples/sql/overview/basicbrowsing2/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/basicbrowsing2/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/basicbrowsing2/basicbrowsing2.pro b/examples/sql/overview/basicbrowsing2/basicbrowsing2.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/basicbrowsing2/basicbrowsing2.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/basicbrowsing2/main.cpp b/examples/sql/overview/basicbrowsing2/main.cpp new file mode 100644 index 0000000..00cedaf --- /dev/null +++ b/examples/sql/overview/basicbrowsing2/main.cpp @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlDatabase *oracledb = QSqlDatabase::database( "ORACLE" ); + // Copy data from the oracle database to the ODBC (default) + // database + QSqlQuery target; + QSqlQuery query( "SELECT id, name FROM people", oracledb ); + int count = 0; + if ( query.isActive() ) { + while ( query.next() ) { + target.exec( "INSERT INTO people ( id, name ) VALUES ( " + + query.value(0).toString() + + ", '" + query.value(1).toString() + "' )" ); + if ( target.isActive() ) + count += target.numRowsAffected(); + } + } + } + + return 0; +} + + + + + diff --git a/examples/sql/overview/basicdatamanip/README b/examples/sql/overview/basicdatamanip/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/basicdatamanip/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/basicdatamanip/basicdatamanip.pro b/examples/sql/overview/basicdatamanip/basicdatamanip.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/basicdatamanip/basicdatamanip.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/basicdatamanip/main.cpp b/examples/sql/overview/basicdatamanip/main.cpp new file mode 100644 index 0000000..56d5a29 --- /dev/null +++ b/examples/sql/overview/basicdatamanip/main.cpp @@ -0,0 +1,40 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +bool createConnections(); + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + int rows = 0; + + if ( createConnections() ) { + QSqlQuery query( "INSERT INTO staff ( id, forename, surname, salary ) " + "VALUES ( 1155, 'Ginger', 'Davis', 50000 )" ); + if ( query.isActive() ) rows += query.numRowsAffected() ; + + query.exec( "UPDATE staff SET salary=60000 WHERE id=1155" ); + if ( query.isActive() ) rows += query.numRowsAffected() ; + + query.exec( "DELETE FROM staff WHERE id=1155" ); + if ( query.isActive() ) rows += query.numRowsAffected() ; + } + + return ( rows == 3 ) ? 0 : 1; +} + + + diff --git a/examples/sql/overview/connect1/README b/examples/sql/overview/connect1/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/connect1/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/connect1/connect1.pro b/examples/sql/overview/connect1/connect1.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/connect1/connect1.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/connect1/main.cpp b/examples/sql/overview/connect1/main.cpp new file mode 100644 index 0000000..c904902 --- /dev/null +++ b/examples/sql/overview/connect1/main.cpp @@ -0,0 +1,31 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include "../connection.h" + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + QSqlDatabase *defaultDB = QSqlDatabase::addDatabase( DB_SALES_DRIVER ); + defaultDB->setDatabaseName( DB_SALES_DBNAME ); + defaultDB->setUserName( DB_SALES_USER ); + defaultDB->setPassword( DB_SALES_PASSWD ); + defaultDB->setHostName( DB_SALES_HOST ); + + if ( defaultDB->open() ) { + // Database successfully opened; we can now issue SQL commands. + } + + return 0; +} + diff --git a/examples/sql/overview/connection.cpp b/examples/sql/overview/connection.cpp new file mode 100644 index 0000000..6afaa53 --- /dev/null +++ b/examples/sql/overview/connection.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ +#include <qsqldatabase.h> +#include "connection.h" + +bool createConnections() +{ + + QSqlDatabase *defaultDB = QSqlDatabase::addDatabase( DB_SALES_DRIVER ); + defaultDB->setDatabaseName( DB_SALES_DBNAME ); + defaultDB->setUserName( DB_SALES_USER ); + defaultDB->setPassword( DB_SALES_PASSWD ); + defaultDB->setHostName( DB_SALES_HOST ); + if ( ! defaultDB->open() ) { + qWarning( "Failed to open sales database: " + defaultDB->lastError().text() ); + return FALSE; + } + + QSqlDatabase *oracle = QSqlDatabase::addDatabase( DB_ORDERS_DRIVER, "ORACLE" ); + oracle->setDatabaseName( DB_ORDERS_DBNAME ); + oracle->setUserName( DB_ORDERS_USER ); + oracle->setPassword( DB_ORDERS_PASSWD ); + oracle->setHostName( DB_ORDERS_HOST ); + if ( ! oracle->open() ) { + qWarning( "Failed to open orders database: " + oracle->lastError().text() ); + return FALSE; + } + + QSqlQuery q(QString::null, defaultDB); + q.exec("create table people (id integer primary key, name char(40))"); + q.exec("create table staff (id integer primary key, forename char(40), " + "surname char(40), salary float, statusid integer)"); + q.exec("create table status (id integer primary key, name char(30))"); + q.exec("create table creditors (id integer primary key, forename char(40), " + "surname char(40), city char(30))"); + q.exec("create table prices (id integer primary key, name char(40), price float)"); + q.exec("create table invoiceitem (id integer primary key, " + "pricesid integer, quantity integer, paiddate date)"); + + QSqlQuery q2(QString::null, oracle); + q2.exec("create table people (id integer primary key, name char(40))"); + + return TRUE; +} diff --git a/examples/sql/overview/connection.h b/examples/sql/overview/connection.h new file mode 100644 index 0000000..e38ce6d --- /dev/null +++ b/examples/sql/overview/connection.h @@ -0,0 +1,24 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +// Enter your connection info here + +#define DB_SALES_DRIVER "QSQLITE" +#define DB_SALES_DBNAME ":memory:" +#define DB_SALES_USER "" +#define DB_SALES_PASSWD "" +#define DB_SALES_HOST "" + +#define DB_ORDERS_DRIVER "QSQLITE" +#define DB_ORDERS_DBNAME ":memory:" +#define DB_ORDERS_USER "" +#define DB_ORDERS_PASSWD "" +#define DB_ORDERS_HOST "" + +bool createConnections(); diff --git a/examples/sql/overview/create_connections/README b/examples/sql/overview/create_connections/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/create_connections/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/create_connections/create_connections.pro b/examples/sql/overview/create_connections/create_connections.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/create_connections/create_connections.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/create_connections/main.cpp b/examples/sql/overview/create_connections/main.cpp new file mode 100644 index 0000000..01a60da --- /dev/null +++ b/examples/sql/overview/create_connections/main.cpp @@ -0,0 +1,26 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + // Databases successfully opened; get pointers to them: + QSqlDatabase *oracledb = QSqlDatabase::database( "ORACLE" ); + // Now we can now issue SQL commands to the oracle connection + // or to the default connection + } + + return 0; +} diff --git a/examples/sql/overview/custom1/custom1.pro b/examples/sql/overview/custom1/custom1.pro new file mode 100644 index 0000000..c490bed --- /dev/null +++ b/examples/sql/overview/custom1/custom1.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = main.h +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/custom1/main.cpp b/examples/sql/overview/custom1/main.cpp new file mode 100644 index 0000000..79c6ffe --- /dev/null +++ b/examples/sql/overview/custom1/main.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" + + +CustomEdit::CustomEdit( QWidget *parent, const char *name ) : + QLineEdit( parent, name ) +{ + connect( this, SIGNAL(textChanged(const QString &)), + this, SLOT(changed(const QString &)) ); +} + + +void CustomEdit::changed( const QString &line ) +{ + setUpperLine( line ); +} + + +void CustomEdit::setUpperLine( const QString &line ) +{ + upperLineText = line.upper(); + setText( upperLineText ); +} + + +QString CustomEdit::upperLine() const +{ + return upperLineText; +} + + +FormDialog::FormDialog() +{ + QLabel *forenameLabel = new QLabel( "Forename:", this ); + CustomEdit *forenameEdit = new CustomEdit( this ); + QLabel *surnameLabel = new QLabel( "Surname:", this ); + CustomEdit *surnameEdit = new CustomEdit( this ); + QLabel *salaryLabel = new QLabel( "Salary:", this ); + QLineEdit *salaryEdit = new QLineEdit( this ); + salaryEdit->setAlignment( Qt::AlignRight ); + QPushButton *saveButton = new QPushButton( "&Save", this ); + connect( saveButton, SIGNAL(clicked()), this, SLOT(save()) ); + + QGridLayout *grid = new QGridLayout( this ); + grid->addWidget( forenameLabel, 0, 0 ); + grid->addWidget( forenameEdit, 0, 1 ); + grid->addWidget( surnameLabel, 1, 0 ); + grid->addWidget( surnameEdit, 1, 1 ); + grid->addWidget( salaryLabel, 2, 0 ); + grid->addWidget( salaryEdit, 2, 1 ); + grid->addWidget( saveButton, 3, 0 ); + grid->activate(); + + staffCursor = new QSqlCursor( "staff" ); + staffCursor->setTrimmed( "forename", TRUE ); + staffCursor->setTrimmed( "surname", TRUE ); + idIndex = staffCursor->index( "id" ); + staffCursor->select( idIndex ); + staffCursor->first(); + + propMap = new QSqlPropertyMap; + propMap->insert( forenameEdit->className(), "upperLine" ); + + sqlForm = new QSqlForm( this ); + sqlForm->setRecord( staffCursor->primeUpdate() ); + sqlForm->installPropertyMap( propMap ); + sqlForm->insert( forenameEdit, "forename" ); + sqlForm->insert( surnameEdit, "surname" ); + sqlForm->insert( salaryEdit, "salary" ); + sqlForm->readFields(); +} + + +FormDialog::~FormDialog() +{ + delete staffCursor; +} + + +void FormDialog::save() +{ + sqlForm->writeFields(); + staffCursor->update(); + staffCursor->select( idIndex ); + staffCursor->first(); +} + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( ! createConnections() ) + return 1; + + FormDialog *formDialog = new FormDialog(); + formDialog->show(); + app.setMainWidget( formDialog ); + + return app.exec(); +} diff --git a/examples/sql/overview/custom1/main.h b/examples/sql/overview/custom1/main.h new file mode 100644 index 0000000..ecbcd35 --- /dev/null +++ b/examples/sql/overview/custom1/main.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qdialog.h> +#include <qlabel.h> +#include <qlayout.h> +#include <qlineedit.h> +#include <qpushbutton.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qsqlform.h> +#include <qsqlpropertymap.h> +#include "../connection.h" + +class CustomEdit : public QLineEdit +{ + Q_OBJECT + Q_PROPERTY( QString upperLine READ upperLine WRITE setUpperLine ) + public: + CustomEdit( QWidget *parent=0, const char *name=0 ); + QString upperLine() const; + void setUpperLine( const QString &line ); + public slots: + void changed( const QString &line ); + private: + QString upperLineText; +}; + + +class FormDialog : public QDialog +{ + Q_OBJECT + public: + FormDialog(); + ~FormDialog(); + public slots: + void save(); + private: + QSqlCursor *staffCursor; + QSqlForm *sqlForm; + QSqlPropertyMap *propMap; + QSqlIndex idIndex; +}; + + diff --git a/examples/sql/overview/delete/README b/examples/sql/overview/delete/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/delete/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/delete/delete.pro b/examples/sql/overview/delete/delete.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/delete/delete.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/delete/main.cpp b/examples/sql/overview/delete/main.cpp new file mode 100644 index 0000000..94aeb32 --- /dev/null +++ b/examples/sql/overview/delete/main.cpp @@ -0,0 +1,29 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlCursor cur( "prices" ); + cur.select( "id=999" ); + if ( cur.next() ) { + cur.primeDelete(); + cur.del(); + } + } + + return 0; +} diff --git a/examples/sql/overview/extract/README b/examples/sql/overview/extract/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/extract/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/extract/extract.pro b/examples/sql/overview/extract/extract.pro new file mode 100644 index 0000000..c517268 --- /dev/null +++ b/examples/sql/overview/extract/extract.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH =../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/extract/main.cpp b/examples/sql/overview/extract/main.cpp new file mode 100644 index 0000000..3714c1a --- /dev/null +++ b/examples/sql/overview/extract/main.cpp @@ -0,0 +1,41 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlCursor cur( "creditors" ); + + QStringList orderFields = QStringList() << "surname" << "forename"; + QSqlIndex order = cur.index( orderFields ); + + QStringList filterFields = QStringList() << "surname" << "city"; + QSqlIndex filter = cur.index( filterFields ); + cur.setValue( "surname", "Chirac" ); + cur.setValue( "city", "Paris" ); + + cur.select( filter, order ); + + while ( cur.next() ) { + int id = cur.value( "id" ).toInt(); + QString name = cur.value( "forename" ).toString() + " " + + cur.value( "surname" ).toString(); + qDebug( QString::number( id ) + ": " + name ); + } + } + + return 0; +} diff --git a/examples/sql/overview/form1/form1.pro b/examples/sql/overview/form1/form1.pro new file mode 100644 index 0000000..a6cd8cb --- /dev/null +++ b/examples/sql/overview/form1/form1.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/form1/main.cpp b/examples/sql/overview/form1/main.cpp new file mode 100644 index 0000000..09b9f4c --- /dev/null +++ b/examples/sql/overview/form1/main.cpp @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qdialog.h> +#include <qlabel.h> +#include <qlayout.h> +#include <qlineedit.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qsqlform.h> +#include "../connection.h" + +class FormDialog : public QDialog +{ + public: + FormDialog(); +}; + + +FormDialog::FormDialog() +{ + QLabel *forenameLabel = new QLabel( "Forename:", this ); + QLabel *forenameDisplay = new QLabel( this ); + QLabel *surnameLabel = new QLabel( "Surname:", this ); + QLabel *surnameDisplay = new QLabel( this ); + QLabel *salaryLabel = new QLabel( "Salary:", this ); + QLineEdit *salaryEdit = new QLineEdit( this ); + + QGridLayout *grid = new QGridLayout( this ); + grid->addWidget( forenameLabel, 0, 0 ); + grid->addWidget( forenameDisplay, 0, 1 ); + grid->addWidget( surnameLabel, 1, 0 ); + grid->addWidget( surnameDisplay, 1, 1 ); + grid->addWidget( salaryLabel, 2, 0 ); + grid->addWidget( salaryEdit, 2, 1 ); + grid->activate(); + + QSqlCursor staffCursor( "staff" ); + staffCursor.select(); + staffCursor.next(); + + QSqlForm sqlForm( this ); + sqlForm.setRecord( staffCursor.primeUpdate() ); + sqlForm.insert( forenameDisplay, "forename" ); + sqlForm.insert( surnameDisplay, "surname" ); + sqlForm.insert( salaryEdit, "salary" ); + sqlForm.readFields(); +} + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( ! createConnections() ) return 1; + + FormDialog *formDialog = new FormDialog(); + formDialog->show(); + app.setMainWidget( formDialog ); + + return app.exec(); +} diff --git a/examples/sql/overview/form2/form2.pro b/examples/sql/overview/form2/form2.pro new file mode 100644 index 0000000..c490bed --- /dev/null +++ b/examples/sql/overview/form2/form2.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = main.h +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/form2/main.cpp b/examples/sql/overview/form2/main.cpp new file mode 100644 index 0000000..80bce49 --- /dev/null +++ b/examples/sql/overview/form2/main.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" + +FormDialog::FormDialog() + : staffCursor( "staff" ) +{ + staffCursor.setTrimmed( "forename", TRUE ); + staffCursor.setTrimmed( "surname", TRUE ); + + QLabel *forenameLabel = new QLabel( "Forename:", this ); + QLineEdit *forenameEdit = new QLineEdit( this ); + QLabel *surnameLabel = new QLabel( "Surname:", this ); + QLineEdit *surnameEdit = new QLineEdit( this ); + QLabel *salaryLabel = new QLabel( "Salary:", this ); + QLineEdit *salaryEdit = new QLineEdit( this ); + QPushButton *saveButton = new QPushButton( "&Save", this ); + connect( saveButton, SIGNAL(clicked()), this, SLOT(save()) ); + + QGridLayout *grid = new QGridLayout( this ); + grid->addWidget( forenameLabel, 0, 0 ); + grid->addWidget( forenameEdit, 0, 1 ); + grid->addWidget( surnameLabel, 1, 0 ); + grid->addWidget( surnameEdit, 1, 1 ); + grid->addWidget( salaryLabel, 2, 0 ); + grid->addWidget( salaryEdit, 2, 1 ); + grid->addWidget( saveButton, 3, 0 ); + grid->activate(); + + idIndex = staffCursor.index( "id" ); + staffCursor.select( idIndex ); + staffCursor.first(); + + sqlForm = new QSqlForm( this ); + sqlForm->setRecord( staffCursor.primeUpdate() ); + sqlForm->insert( forenameEdit, "forename" ); + sqlForm->insert( surnameEdit, "surname" ); + sqlForm->insert( salaryEdit, "salary" ); + sqlForm->readFields(); +} + + +FormDialog::~FormDialog() +{ + +} + + +void FormDialog::save() +{ + sqlForm->writeFields(); + staffCursor.update(); + staffCursor.select( idIndex ); + staffCursor.first(); +} + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( ! createConnections() ) + return 1; + + FormDialog *formDialog = new FormDialog(); + formDialog->show(); + app.setMainWidget( formDialog ); + + return app.exec(); +} diff --git a/examples/sql/overview/form2/main.h b/examples/sql/overview/form2/main.h new file mode 100644 index 0000000..c19a354 --- /dev/null +++ b/examples/sql/overview/form2/main.h @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qdialog.h> +#include <qlabel.h> +#include <qlayout.h> +#include <qlineedit.h> +#include <qpushbutton.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qsqlform.h> +#include "../connection.h" + +class FormDialog : public QDialog +{ + Q_OBJECT + public: + FormDialog(); + ~FormDialog(); + public slots: + void save(); + private: + QSqlCursor staffCursor; + QSqlForm *sqlForm; + QSqlIndex idIndex; +}; diff --git a/examples/sql/overview/insert/README b/examples/sql/overview/insert/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/insert/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/insert/insert.pro b/examples/sql/overview/insert/insert.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/insert/insert.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/insert/main.cpp b/examples/sql/overview/insert/main.cpp new file mode 100644 index 0000000..7146bd1 --- /dev/null +++ b/examples/sql/overview/insert/main.cpp @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + int count = 0; + QSqlCursor cur( "prices" ); + QStringList names = QStringList() << + "Screwdriver" << "Hammer" << "Wrench" << "Saw"; + int id = 20; + for ( QStringList::Iterator name = names.begin(); + name != names.end(); ++name ) { + QSqlRecord *buffer = cur.primeInsert(); + buffer->setValue( "id", id ); + buffer->setValue( "name", *name ); + buffer->setValue( "price", 100.0 + (double)id ); + count += cur.insert(); + id++; + } + } + + return 0; +} diff --git a/examples/sql/overview/insert2/insert2.pro b/examples/sql/overview/insert2/insert2.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/insert2/insert2.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/insert2/main.cpp b/examples/sql/overview/insert2/main.cpp new file mode 100644 index 0000000..1b2ecdf --- /dev/null +++ b/examples/sql/overview/insert2/main.cpp @@ -0,0 +1,29 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlCursor cur( "prices" ); + QSqlRecord *buffer = cur.primeInsert(); + buffer->setValue( "id", 53981 ); + buffer->setValue( "name", "Thingy" ); + buffer->setValue( "price", 105.75 ); + cur.insert(); + } + + return 0; +} diff --git a/examples/sql/overview/navigating/README b/examples/sql/overview/navigating/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/navigating/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/navigating/main.cpp b/examples/sql/overview/navigating/main.cpp new file mode 100644 index 0000000..26567bf --- /dev/null +++ b/examples/sql/overview/navigating/main.cpp @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlQuery query( "SELECT id, name FROM people ORDER BY name" ); + if ( ! query.isActive() ) return 1; // Query failed + int i; + i = query.size(); // In this example we have 9 records; i == 9. + query.first(); // Moves to the first record. + i = query.at(); // i == 0 + query.last(); // Moves to the last record. + i = query.at(); // i == 8 + query.seek( query.size() / 2 ); // Moves to the middle record. + i = query.at(); // i == 4 + } + + return 0; +} diff --git a/examples/sql/overview/navigating/navigating.pro b/examples/sql/overview/navigating/navigating.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/navigating/navigating.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/order1/README b/examples/sql/overview/order1/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/order1/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/order1/main.cpp b/examples/sql/overview/order1/main.cpp new file mode 100644 index 0000000..3d64748 --- /dev/null +++ b/examples/sql/overview/order1/main.cpp @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlCursor cur( "staff" ); + QStringList fields = QStringList() << "surname" << "forename"; + QSqlIndex order = cur.index( fields ); + cur.select( order ); + while ( cur.next() ) { + qDebug( cur.value( "id" ).toString() + ": " + + cur.value( "surname" ).toString() + " " + + cur.value( "forename" ).toString() ); + } + } + + return 0; +} diff --git a/examples/sql/overview/order1/order1.pro b/examples/sql/overview/order1/order1.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/order1/order1.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/order2/README b/examples/sql/overview/order2/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/order2/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/order2/main.cpp b/examples/sql/overview/order2/main.cpp new file mode 100644 index 0000000..762562e --- /dev/null +++ b/examples/sql/overview/order2/main.cpp @@ -0,0 +1,34 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlCursor cur( "staff" ); + QStringList fields = QStringList() << "id" << "forename"; + QSqlIndex order = cur.index( fields ); + QSqlIndex filter = cur.index( "surname" ); + cur.setValue( "surname", "Bloggs" ); + cur.select( filter, order ); + while ( cur.next() ) { + qDebug( cur.value( "id" ).toString() + ": " + + cur.value( "surname" ).toString() + " " + + cur.value( "forename" ).toString() ); + } + } + + return 0; +} diff --git a/examples/sql/overview/order2/order2.pro b/examples/sql/overview/order2/order2.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/order2/order2.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/overview.pro b/examples/sql/overview/overview.pro new file mode 100644 index 0000000..59be651 --- /dev/null +++ b/examples/sql/overview/overview.pro @@ -0,0 +1,33 @@ +TEMPLATE = subdirs + +CONFIG += ordered + +REQUIRES = full-config + +SUBDIRS = basicbrowsing \ + basicbrowsing2 \ + basicdatamanip \ + connect1 \ + create_connections \ + custom1 \ + delete \ + extract \ + form1 \ + form2 \ + insert \ + insert2 \ + navigating \ + order1 \ + order2 \ + retrieve1 \ + retrieve2 \ + subclass1 \ + subclass2 \ + subclass3 \ + subclass4 \ + subclass5 \ + table1 \ + table2 \ + table3 \ + table4 \ + update diff --git a/examples/sql/overview/retrieve1/README b/examples/sql/overview/retrieve1/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/retrieve1/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/retrieve1/main.cpp b/examples/sql/overview/retrieve1/main.cpp new file mode 100644 index 0000000..b1cc06b --- /dev/null +++ b/examples/sql/overview/retrieve1/main.cpp @@ -0,0 +1,30 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlQuery query( "SELECT id, surname FROM staff" ); + if ( query.isActive() ) { + while ( query.next() ) { + qDebug( query.value(0).toString() + ": " + + query.value(1).toString() ); + } + } + } + + return 0; +} diff --git a/examples/sql/overview/retrieve1/retrieve1.pro b/examples/sql/overview/retrieve1/retrieve1.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/retrieve1/retrieve1.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/retrieve2/README b/examples/sql/overview/retrieve2/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/retrieve2/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/retrieve2/main.cpp b/examples/sql/overview/retrieve2/main.cpp new file mode 100644 index 0000000..69b172b --- /dev/null +++ b/examples/sql/overview/retrieve2/main.cpp @@ -0,0 +1,30 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + QSqlCursor cur( "staff" ); // Specify the table/view name + cur.select(); // We'll retrieve every record + while ( cur.next() ) { + qDebug( cur.value( "id" ).toString() + ": " + + cur.value( "surname" ).toString() + " " + + cur.value( "salary" ).toString() ); + } + } + + return 0; +} diff --git a/examples/sql/overview/retrieve2/retrieve2.pro b/examples/sql/overview/retrieve2/retrieve2.pro new file mode 100644 index 0000000..c58585e --- /dev/null +++ b/examples/sql/overview/retrieve2/retrieve2.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass1/main.cpp b/examples/sql/overview/subclass1/main.cpp new file mode 100644 index 0000000..4d82695 --- /dev/null +++ b/examples/sql/overview/subclass1/main.cpp @@ -0,0 +1,38 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qdatatable.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + QSqlCursor invoiceItemCursor( "invoiceitem" ); + + QDataTable *invoiceItemTable = new QDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "pricesid", "PriceID" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass1/subclass1.pro b/examples/sql/overview/subclass1/subclass1.pro new file mode 100644 index 0000000..a6cd8cb --- /dev/null +++ b/examples/sql/overview/subclass1/subclass1.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass2/main.cpp b/examples/sql/overview/subclass2/main.cpp new file mode 100644 index 0000000..06016ba --- /dev/null +++ b/examples/sql/overview/subclass2/main.cpp @@ -0,0 +1,42 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +InvoiceItemCursor::InvoiceItemCursor() : + QSqlCursor( "invoiceitem" ) +{ + // NOOP +} + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + InvoiceItemCursor invoiceItemCursor; + + QDataTable *invoiceItemTable = new QDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "pricesid", "PriceID" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass2/main.h b/examples/sql/overview/subclass2/main.h new file mode 100644 index 0000000..024a651 --- /dev/null +++ b/examples/sql/overview/subclass2/main.h @@ -0,0 +1,23 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +class QSqlRecord; + +class InvoiceItemCursor : public QSqlCursor +{ + public: + InvoiceItemCursor(); +}; + + diff --git a/examples/sql/overview/subclass2/subclass2.pro b/examples/sql/overview/subclass2/subclass2.pro new file mode 100644 index 0000000..a6cd8cb --- /dev/null +++ b/examples/sql/overview/subclass2/subclass2.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass3/main.cpp b/examples/sql/overview/subclass3/main.cpp new file mode 100644 index 0000000..84f62ec --- /dev/null +++ b/examples/sql/overview/subclass3/main.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +InvoiceItemCursor::InvoiceItemCursor() : + QSqlCursor( "invoiceitem" ) +{ + QSqlFieldInfo productName( "productname", QVariant::String ); + append( productName ); + setCalculated( productName.name(), TRUE ); +} + + +QVariant InvoiceItemCursor::calculateField( const QString & name ) +{ + if ( name == "productname" ) { + QSqlQuery query( "SELECT name FROM prices WHERE id=" + + field( "pricesid" )->value().toString() ); + if ( query.next() ) + return query.value( 0 ); + } + + return QVariant( QString::null ); +} + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + InvoiceItemCursor invoiceItemCursor; + + QDataTable *invoiceItemTable = new QDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "productname", "Product" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass3/main.h b/examples/sql/overview/subclass3/main.h new file mode 100644 index 0000000..d84344a --- /dev/null +++ b/examples/sql/overview/subclass3/main.h @@ -0,0 +1,25 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +class QSqlRecord; + +class InvoiceItemCursor : public QSqlCursor +{ + public: + InvoiceItemCursor(); + protected: + QVariant calculateField( const QString & name ); +}; + + diff --git a/examples/sql/overview/subclass3/subclass3.pro b/examples/sql/overview/subclass3/subclass3.pro new file mode 100644 index 0000000..a6cd8cb --- /dev/null +++ b/examples/sql/overview/subclass3/subclass3.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass4/main.cpp b/examples/sql/overview/subclass4/main.cpp new file mode 100644 index 0000000..9a07eb0 --- /dev/null +++ b/examples/sql/overview/subclass4/main.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +InvoiceItemCursor::InvoiceItemCursor() : + QSqlCursor( "invoiceitem" ) +{ + QSqlFieldInfo productName( "productname", QVariant::String ); + append( productName ); + setCalculated( productName.name(), TRUE ); + + QSqlFieldInfo productPrice( "price", QVariant::Double ); + append( productPrice ); + setCalculated( productPrice.name(), TRUE ); + + QSqlFieldInfo productCost( "cost", QVariant::Double ); + append( productCost ); + setCalculated( productCost.name(), TRUE ); +} + + +QVariant InvoiceItemCursor::calculateField( const QString & name ) +{ + + if ( name == "productname" ) { + QSqlQuery query( "SELECT name FROM prices WHERE id=" + + field( "pricesid" )->value().toString() ); + if ( query.next() ) + return query.value( 0 ); + } + else if ( name == "price" ) { + QSqlQuery query( "SELECT price FROM prices WHERE id=" + + field( "pricesid" )->value().toString() ); + if ( query.next() ) + return query.value( 0 ); + } + else if ( name == "cost" ) { + QSqlQuery query( "SELECT price FROM prices WHERE id=" + + field( "pricesid" )->value().toString() ); + if ( query.next() ) + return QVariant( query.value( 0 ).toDouble() * + value( "quantity").toDouble() ); + } + + return QVariant( QString::null ); +} + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + InvoiceItemCursor invoiceItemCursor; + + QDataTable *invoiceItemTable = new QDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "productname", "Product" ); + invoiceItemTable->addColumn( "price", "Price" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "cost", "Cost" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass4/main.h b/examples/sql/overview/subclass4/main.h new file mode 100644 index 0000000..d84344a --- /dev/null +++ b/examples/sql/overview/subclass4/main.h @@ -0,0 +1,25 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +class QSqlRecord; + +class InvoiceItemCursor : public QSqlCursor +{ + public: + InvoiceItemCursor(); + protected: + QVariant calculateField( const QString & name ); +}; + + diff --git a/examples/sql/overview/subclass4/subclass4.pro b/examples/sql/overview/subclass4/subclass4.pro new file mode 100644 index 0000000..a6cd8cb --- /dev/null +++ b/examples/sql/overview/subclass4/subclass4.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/subclass5/main.cpp b/examples/sql/overview/subclass5/main.cpp new file mode 100644 index 0000000..5c318a9 --- /dev/null +++ b/examples/sql/overview/subclass5/main.cpp @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +InvoiceItemCursor::InvoiceItemCursor() : + QSqlCursor( "invoiceitem" ) +{ + QSqlFieldInfo productName( "productname", QVariant::String ); + append( productName ); + setCalculated( productName.name(), TRUE ); + + QSqlFieldInfo productPrice( "price", QVariant::Double ); + append( productPrice ); + setCalculated( productPrice.name(), TRUE ); + + QSqlFieldInfo productCost( "cost", QVariant::Double ); + append( productCost ); + setCalculated( productCost.name(), TRUE ); +} + + +QVariant InvoiceItemCursor::calculateField( const QString & name ) +{ + + if ( name == "productname" ) { + QSqlQuery query( "SELECT name FROM prices WHERE id=" + + field( "pricesid" )->value().toString() ); + if ( query.next() ) + return query.value( 0 ); + } + else if ( name == "price" ) { + QSqlQuery query( "SELECT price FROM prices WHERE id=" + + field( "pricesid" )->value().toString() ); + if ( query.next() ) + return query.value( 0 ); + } + else if ( name == "cost" ) { + QSqlQuery query( "SELECT price FROM prices WHERE id=" + + field( "pricesid" )->value().toString() ); + if ( query.next() ) + return QVariant( query.value( 0 ).toDouble() * + value( "quantity").toDouble() ); + } + + return QVariant( QString::null ); +} + + +QSqlRecord *InvoiceItemCursor::primeInsert() +{ + QSqlRecord *buffer = editBuffer(); + QSqlQuery query( "SELECT NEXTVAL( 'invoiceitem_seq' )" ); + if ( query.next() ) + buffer->setValue( "id", query.value( 0 ) ); + buffer->setValue( "paiddate", QDate::currentDate() ); + buffer->setValue( "quantity", 1 ); + + return buffer; +} + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + InvoiceItemCursor invoiceItemCursor; + + QDataTable *invoiceItemTable = new QDataTable( &invoiceItemCursor ); + + app.setMainWidget( invoiceItemTable ); + + invoiceItemTable->addColumn( "productname", "Product" ); + invoiceItemTable->addColumn( "price", "Price" ); + invoiceItemTable->addColumn( "quantity", "Quantity" ); + invoiceItemTable->addColumn( "cost", "Cost" ); + invoiceItemTable->addColumn( "paiddate", "Paid" ); + + invoiceItemTable->refresh(); + invoiceItemTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/subclass5/main.h b/examples/sql/overview/subclass5/main.h new file mode 100644 index 0000000..48dab4e --- /dev/null +++ b/examples/sql/overview/subclass5/main.h @@ -0,0 +1,25 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qdatetime.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +class QSqlRecord; + +class InvoiceItemCursor : public QSqlCursor +{ + public: + InvoiceItemCursor(); + QSqlRecord *primeInsert(); + protected: + QVariant calculateField( const QString & name ); +}; diff --git a/examples/sql/overview/subclass5/subclass5.pro b/examples/sql/overview/subclass5/subclass5.pro new file mode 100644 index 0000000..a6cd8cb --- /dev/null +++ b/examples/sql/overview/subclass5/subclass5.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/table1/main.cpp b/examples/sql/overview/table1/main.cpp new file mode 100644 index 0000000..2334f54 --- /dev/null +++ b/examples/sql/overview/table1/main.cpp @@ -0,0 +1,31 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qdatatable.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + QSqlCursor staffCursor( "staff" ); + QDataTable *staffTable = new QDataTable( &staffCursor, TRUE ); + app.setMainWidget( staffTable ); + staffTable->refresh(); + staffTable->show(); + + return app.exec(); + } + + return 0; +} diff --git a/examples/sql/overview/table1/table1.pro b/examples/sql/overview/table1/table1.pro new file mode 100644 index 0000000..a6cd8cb --- /dev/null +++ b/examples/sql/overview/table1/table1.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/table2/main.cpp b/examples/sql/overview/table2/main.cpp new file mode 100644 index 0000000..2bc40eb --- /dev/null +++ b/examples/sql/overview/table2/main.cpp @@ -0,0 +1,41 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qdatatable.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + QSqlCursor staffCursor( "staff" ); + + QDataTable *staffTable = new QDataTable( &staffCursor ); + + app.setMainWidget( staffTable ); + + staffTable->addColumn( "forename", "Forename" ); + staffTable->addColumn( "surname", "Surname" ); + staffTable->addColumn( "salary", "Annual Salary" ); + + QStringList order = QStringList() << "surname" << "forename"; + staffTable->setSort( order ); + + staffTable->refresh(); + staffTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/table2/table2.pro b/examples/sql/overview/table2/table2.pro new file mode 100644 index 0000000..a6cd8cb --- /dev/null +++ b/examples/sql/overview/table2/table2.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/table3/main.cpp b/examples/sql/overview/table3/main.cpp new file mode 100644 index 0000000..03d0729 --- /dev/null +++ b/examples/sql/overview/table3/main.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" +#include <qdatatable.h> + +StatusPicker::StatusPicker( QWidget *parent, const char *name ) + : QComboBox( parent, name ) +{ + QSqlCursor cur( "status" ); + cur.select( cur.index( "name" ) ); + + int i = 0; + while ( cur.next() ) { + insertItem( cur.value( "name" ).toString(), i ); + index2id[i] = cur.value( "id" ).toInt(); + i++; + } +} + + +int StatusPicker::statusId() const +{ + return index2id[ currentItem() ]; +} + + +void StatusPicker::setStatusId( int statusid ) +{ + QMap<int,int>::Iterator it; + for ( it = index2id.begin(); it != index2id.end(); ++it ) { + if ( it.data() == statusid ) { + setCurrentItem( it.key() ); + break; + } + } +} + + + +QWidget *CustomSqlEditorFactory::createEditor( + QWidget *parent, const QSqlField *field ) +{ + if ( field->name() == "statusid" ) { + QWidget *editor = new StatusPicker( parent ); + return editor; + } + + return QSqlEditorFactory::createEditor( parent, field ); +} + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + QSqlCursor staffCursor( "staff" ); + + QDataTable *staffTable = new QDataTable( &staffCursor ); + QSqlPropertyMap *propMap = new QSqlPropertyMap(); + CustomSqlEditorFactory *editorFactory = new CustomSqlEditorFactory(); + propMap->insert( "StatusPicker", "statusid" ); + staffTable->installPropertyMap( propMap ); + staffTable->installEditorFactory( editorFactory ); + + app.setMainWidget( staffTable ); + + staffTable->addColumn( "forename", "Forename" ); + staffTable->addColumn( "surname", "Surname" ); + staffTable->addColumn( "salary", "Annual Salary" ); + staffTable->addColumn( "statusid", "Status" ); + + QStringList order = QStringList() << "surname" << "forename"; + staffTable->setSort( order ); + + staffTable->refresh(); + staffTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/table3/main.h b/examples/sql/overview/table3/main.h new file mode 100644 index 0000000..3cee08d --- /dev/null +++ b/examples/sql/overview/table3/main.h @@ -0,0 +1,40 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qcombobox.h> +#include <qmap.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qsqleditorfactory.h> +#include <qsqlpropertymap.h> +#include "../connection.h" + +class StatusPicker : public QComboBox +{ + Q_OBJECT + Q_PROPERTY( int statusid READ statusId WRITE setStatusId ) + public: + StatusPicker( QWidget *parent=0, const char *name=0 ); + int statusId() const; + void setStatusId( int id ); + private: + QMap< int, int > index2id; +}; + + +class CustomSqlEditorFactory : public QSqlEditorFactory +{ + Q_OBJECT + public: + QWidget *createEditor( QWidget *parent, const QSqlField *field ); +}; + + + diff --git a/examples/sql/overview/table3/table3.pro b/examples/sql/overview/table3/table3.pro new file mode 100644 index 0000000..c490bed --- /dev/null +++ b/examples/sql/overview/table3/table3.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = main.h +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/table4/main.cpp b/examples/sql/overview/table4/main.cpp new file mode 100644 index 0000000..1276c3d --- /dev/null +++ b/examples/sql/overview/table4/main.cpp @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include "main.h" + + +StatusPicker::StatusPicker( QWidget *parent, const char *name ) + : QComboBox( parent, name ) +{ + QSqlCursor cur( "status" ); + cur.select( cur.index( "name" ) ); + + int i = 0; + while ( cur.next() ) { + insertItem( cur.value( "name" ).toString(), i ); + index2id[i] = cur.value( "id" ).toInt(); + i++; + } +} + + +int StatusPicker::statusId() const +{ + return index2id[ currentItem() ]; +} + + +void StatusPicker::setStatusId( int statusid ) +{ + QMap<int,int>::Iterator it; + for ( it = index2id.begin(); it != index2id.end(); ++it ) { + if ( it.data() == statusid ) { + setCurrentItem( it.key() ); + break; + } + } +} + + +void CustomTable::paintField( QPainter * p, const QSqlField* field, + const QRect & cr, bool b) +{ + if ( !field ) + return; + if ( field->name() == "statusid" ) { + QSqlQuery query( "SELECT name FROM status WHERE id=" + + field->value().toString() ); + QString text; + if ( query.next() ) { + text = query.value( 0 ).toString(); + } + p->drawText( 2,2, cr.width()-4, cr.height()-4, fieldAlignment( field ), text ); + } + else { + QDataTable::paintField( p, field, cr, b) ; + } +} + + +QWidget *CustomSqlEditorFactory::createEditor( + QWidget *parent, const QSqlField *field ) +{ + if ( field->name() == "statusid" ) { + QWidget *editor = new StatusPicker( parent ); + return editor; + } + + return QSqlEditorFactory::createEditor( parent, field ); +} + + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv ); + + if ( createConnections() ) { + QSqlCursor staffCursor( "staff" ); + + CustomTable *staffTable = new CustomTable( &staffCursor ); + QSqlPropertyMap *propMap = new QSqlPropertyMap(); + CustomSqlEditorFactory *editorFactory = new CustomSqlEditorFactory(); + propMap->insert( "StatusPicker", "statusid" ); + staffTable->installPropertyMap( propMap ); + staffTable->installEditorFactory( editorFactory ); + + app.setMainWidget( staffTable ); + + staffTable->addColumn( "forename", "Forename" ); + staffTable->addColumn( "surname", "Surname" ); + staffTable->addColumn( "salary", "Annual Salary" ); + staffTable->addColumn( "statusid", "Status" ); + + QStringList order = QStringList() << "surname" << "forename"; + staffTable->setSort( order ); + + staffTable->refresh(); + staffTable->show(); + + return app.exec(); + } + + return 1; +} diff --git a/examples/sql/overview/table4/main.h b/examples/sql/overview/table4/main.h new file mode 100644 index 0000000..e175713 --- /dev/null +++ b/examples/sql/overview/table4/main.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qcombobox.h> +#include <qmap.h> +#include <qpainter.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include <qsqleditorfactory.h> +#include <qsqlpropertymap.h> +#include <qdatatable.h> +#include "../connection.h" + +class StatusPicker : public QComboBox +{ + Q_OBJECT + Q_PROPERTY( int statusid READ statusId WRITE setStatusId ) +public: + StatusPicker( QWidget *parent=0, const char *name=0 ); + int statusId() const; + void setStatusId( int id ); +private: + QMap< int, int > index2id; +}; + + +class CustomTable : public QDataTable +{ + Q_OBJECT +public: + CustomTable( + QSqlCursor *cursor, bool autoPopulate = FALSE, + QWidget * parent = 0, const char * name = 0 ) : + QDataTable( cursor, autoPopulate, parent, name ) {} + void paintField( + QPainter * p, const QSqlField* field, const QRect & cr, bool ); + +}; + + +class CustomSqlEditorFactory : public QSqlEditorFactory +{ + Q_OBJECT +public: + QWidget *createEditor( QWidget *parent, const QSqlField *field ); +}; diff --git a/examples/sql/overview/table4/table4.pro b/examples/sql/overview/table4/table4.pro new file mode 100644 index 0000000..c490bed --- /dev/null +++ b/examples/sql/overview/table4/table4.pro @@ -0,0 +1,8 @@ +TEMPLATE = app + +CONFIG += qt warn_on release + +REQUIRES = full-config + +HEADERS = main.h +SOURCES = main.cpp ../connection.cpp diff --git a/examples/sql/overview/update/README b/examples/sql/overview/update/README new file mode 100644 index 0000000..eae3cd7 --- /dev/null +++ b/examples/sql/overview/update/README @@ -0,0 +1 @@ +This example is part of the overview in sql.html diff --git a/examples/sql/overview/update/main.cpp b/examples/sql/overview/update/main.cpp new file mode 100644 index 0000000..02cd303 --- /dev/null +++ b/examples/sql/overview/update/main.cpp @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. +** +** This file is part of an example program for Qt. This example +** program may be used, distributed and modified without limitation. +** +*****************************************************************************/ + +#include <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlcursor.h> +#include "../connection.h" + +int main( int argc, char *argv[] ) +{ + QApplication app( argc, argv, FALSE ); + + if ( createConnections() ) { + QSqlCursor cur( "prices" ); + cur.select( "id=202" ); + if ( cur.next() ) { + QSqlRecord *buffer = cur.primeUpdate(); + double price = buffer->value( "price" ).toDouble(); + double newprice = price * 1.05; + buffer->setValue( "price", newprice ); + cur.update(); + } + } + + return 0; +} diff --git a/examples/sql/overview/update/update.pro b/examples/sql/overview/update/update.pro new file mode 100644 index 0000000..9e6df96 --- /dev/null +++ b/examples/sql/overview/update/update.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt warn_on release +DEPENDPATH = ../../../include + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp ../connection.cpp |