I am a newbie and I was trying this code from http://developer.qt.nokia.com/ on QSqlQueryModel

ArtistsSqlModel.h
Qt Code:
  1. #ifndef ARTISTSSQLMODEL_H
  2. #define ARTISTSSQLMODEL_H
  3. #include "database.h"
  4.  
  5. class ArtistsSqlModel : public QSqlQueryModel
  6. {
  7. Q_OBJECT
  8.  
  9. public:
  10. explicit ArtistsSqlModel(QObject *parent);
  11. void refresh();
  12. QVariant data(const QModelIndex &index, int role) const;
  13.  
  14. signals:
  15.  
  16. public slots:
  17.  
  18. private:
  19. const static char* COLUMN_NAMES[];
  20. const static char* SQL_SELECT;
  21. };
  22.  
  23. const char* ArtistsSqlModel::COLUMN_NAMES[] = {
  24. "artist",
  25. "title",
  26. "year",
  27. NULL
  28. };
  29.  
  30. const char* ArtistsSqlModel::SQL_SELECT =
  31. "SELECT artists.artist, albums.title, albums.year"
  32. " FROM albums"
  33. " JOIN artists ON albums.artistid = artists.id";
  34.  
  35. #endif // ARTISTSSQLMODEL_H
To copy to clipboard, switch view to plain text mode 

database.h
Qt Code:
  1. #ifndef DATABASE_H
  2. #define DATABASE_H
  3.  
  4. #include <QMessageBox>
  5. #include <QSqlDatabase>
  6. #include <QSqlError>
  7. #include <QSqlQuery>
  8.  
  9. static bool createConnection()
  10. {
  11. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  12. db.setDatabaseName(":memory:");
  13. if (!db.open()) {
  14. QMessageBox::critical(0, qApp->tr("Cannot open database"),
  15. qApp->tr("Unable to establish a database connection.\n"
  16. "This example needs SQLite support. Please read "
  17. "the Qt SQL driver documentation for information how "
  18. "to build it.\n\n"
  19. "Click Cancel to exit."), QMessageBox::Cancel);
  20. return false;
  21. }
  22.  
  23. QSqlQuery query;
  24.  
  25. query.exec("create table artists (id int primary key, "
  26. "artist varchar(40), "
  27. "albumcount int)");
  28.  
  29. query.exec("insert into artists values(0, '<all>', 0)");
  30. query.exec("insert into artists values(1, 'Ane Brun', 2)");
  31. query.exec("insert into artists values(2, 'Thomas Dybdahl', 3)");
  32. query.exec("insert into artists values(3, 'Kaizers Orchestra', 3)");
  33.  
  34. query.exec("create table albums (albumid int primary key, "
  35. "title varchar(50), "
  36. "artistid int, "
  37. "year int)");
  38.  
  39. query.exec("insert into albums values(1, 'Spending Time With Morgan', 1, "
  40. "2003)");
  41. query.exec("insert into albums values(2, 'A Temporary Dive', 1, 2005)");
  42. query.exec("insert into albums values(3, '...The Great October Sound', 2, "
  43. "2002)");
  44. query.exec("insert into albums values(4, 'Stray Dogs', 2, 2003)");
  45. query.exec("insert into albums values(5, "
  46. "'One day you`ll dance for me, New York City', 2, 2004)");
  47. query.exec("insert into albums values(6, 'Ompa Til Du D\xf8r', 3, 2001)");
  48. query.exec("insert into albums values(7, 'Evig Pint', 3, 2002)");
  49. query.exec("insert into albums values(8, 'Maestro', 3, 2005)");
  50.  
  51. return true;
  52. }
  53.  
  54. #endif
To copy to clipboard, switch view to plain text mode 

ArtistsSqlModel.cpp
Qt Code:
  1. #include "ArtistsSqlModel.h";
  2.  
  3.  
  4. ArtistsSqlModel::ArtistsSqlModel(QObject *parent) :
  5. {
  6. int idx = 0;
  7. QHash<int, QByteArray> roleNames;
  8. while( COLUMN_NAMES[idx]) {
  9. roleNames[Qt::UserRole + idx + 1] = COLUMN_NAMES[idx];
  10. idx++;
  11. }
  12. setRoleNames(roleNames);
  13. refresh();
  14. }
To copy to clipboard, switch view to plain text mode 

ArtistItemDelegate.qml
Qt Code:
  1. import Qt 4.7
  2.  
  3. Item {
  4. id: delegate
  5. width: delegate.ListView.view.width;
  6. height: 30
  7. clip: true
  8. anchors.margins: 4
  9.  
  10. Row {
  11. anchors.margins: 4
  12. anchors.fill: parent
  13. spacing: 4;
  14.  
  15. Text {
  16. text: artist
  17. width: 150
  18. }
  19.  
  20. Text {
  21. text: title
  22. width: 300;
  23. }
  24.  
  25. Text {
  26. text: year
  27. width: 50;
  28. }
  29. }
  30. }
To copy to clipboard, switch view to plain text mode 

main.qml
Qt Code:
  1. import QtQuick 1.1
  2.  
  3. Rectangle {
  4. width: 360
  5. height: 360
  6.  
  7. ListView {
  8. id: list_view1
  9. clip: true
  10. anchors.margins: 10
  11. anchors.fill: parent
  12. model: artistsModel
  13. delegate: ArtistItemDelegate {}
  14. }
  15.  
  16. }
To copy to clipboard, switch view to plain text mode 

main.cpp
Qt Code:
  1. #include <QtGui/QApplication>
  2. #include "qmlapplicationviewer.h"
  3. #include "ArtistsSqlModel.h"
  4.  
  5. QVariant ArtistsSqlModel::data(const QModelIndex &index, int role) const
  6. {
  7. QVariant value = QSqlQueryModel::data(index, role);
  8. if(role < Qt::UserRole)
  9. {
  10. value = QSqlQueryModel::data(index, role);
  11. }
  12. else
  13. {
  14. int columnIdx = role - Qt::UserRole - 1;
  15. QModelIndex modelIndex = this->index(index.row(), columnIdx);
  16. value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
  17. }
  18. return value;
  19. }
  20.  
  21. Q_DECL_EXPORT int main(int argc, char *argv[])
  22. {
  23. ArtistsSqlModel *artistsSqlModel = new ArtistsSqlModel( qApp);
  24. QmlApplicationViewer viewer;
  25. viewer.rootContext()->setContextProperty("artistsModel", artistsSqlModel);
  26. viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
  27. viewer.setMainQmlFile(QLatin1String("qml/SQLListView/main.qml"));
  28. viewer.showExpanded();
  29. }
To copy to clipboard, switch view to plain text mode 

I was getting this error
Qt Code:
  1. /home/sobingt/QtSDK/Test6/finalQlist-build-desktop-Desktop_Qt_4_8_0_for_GCC__Qt_SDK__Release/../finalQlist/database.h:5: error: QSqlDatabase: No such file or directory
To copy to clipboard, switch view to plain text mode 

I know i must have done a very very stupid mistake.Sorry for wasting your time. but can u help me..