Next: Código fuente programa para Up: Código fuente programas en Previous: Creación de la base Contents

##
Código fuente programa qlistview.cpp

  #include <qlistview.h>

  #include <qsqldatabase.h>

  #include <qsqlquery.h>

  int main( int argc, char* argv[] )

  {

  QApplication app( argc, argv );

  //Connect to the database

  QSqlDatabase* courseDB = QSqlDatabase::addDatabase( "QMYSQL3" );

  if( courseDB ) {

  courseDB->setHostname( "localhost" );

  courseDB->setDatabaseName( "courses" );

  courseDB->setUserName( "zaurus" );

  courseDB->setPassword( "zaurus");

  if( courseDB->open() ) {

  // Database could be opened

  // Create a QListView as the toplevel widget.

  QListView courseLV;

  app.setMainWidget( &courseLV );

  courseLV.addcolumn( "Name" );

  courseLV.addcolumn( "Location" );

  courseLV.addcolumn( "Start Date" );

  courseLV.addcolumn( "End Date" );

  courseLV.show();

  // create a query and execute it

  QSqlQuery query( "SELECT name, location, start_date,end_date FROM courses" );

  if( query.isActive() ) {

  while( query.next() ) {

  new QListViewItem( &courseLV,

  query.value(0).toString(),

  query.value(1).toString(),

  query.value(2).toString(),

  query.value(3).toString() );

  }

  }

  return app.exec();

  } else

  qDebug( "Could not open database " );

  } else

  qDebug( "Could not activate database driver" );

  }