PDA

View Full Version : error: QSqlDatabase: No such file or directory



learningqt
15th February 2012, 06:15
I am a newbie and I was trying this code from http://developer.qt.nokia.com/ on QSqlQueryModel

ArtistsSqlModel.h

#ifndef ARTISTSSQLMODEL_H
#define ARTISTSSQLMODEL_H
#include "database.h"

class ArtistsSqlModel : public QSqlQueryModel
{
Q_OBJECT

public:
explicit ArtistsSqlModel(QObject *parent);
void refresh();
QVariant data(const QModelIndex &index, int role) const;

signals:

public slots:

private:
const static char* COLUMN_NAMES[];
const static char* SQL_SELECT;
};

const char* ArtistsSqlModel::COLUMN_NAMES[] = {
"artist",
"title",
"year",
NULL
};

const char* ArtistsSqlModel::SQL_SELECT =
"SELECT artists.artist, albums.title, albums.year"
" FROM albums"
" JOIN artists ON albums.artistid = artists.id";

#endif // ARTISTSSQLMODEL_H


database.h

#ifndef DATABASE_H
#define DATABASE_H

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>

static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}

QSqlQuery query;

query.exec("create table artists (id int primary key, "
"artist varchar(40), "
"albumcount int)");

query.exec("insert into artists values(0, '<all>', 0)");
query.exec("insert into artists values(1, 'Ane Brun', 2)");
query.exec("insert into artists values(2, 'Thomas Dybdahl', 3)");
query.exec("insert into artists values(3, 'Kaizers Orchestra', 3)");

query.exec("create table albums (albumid int primary key, "
"title varchar(50), "
"artistid int, "
"year int)");

query.exec("insert into albums values(1, 'Spending Time With Morgan', 1, "
"2003)");
query.exec("insert into albums values(2, 'A Temporary Dive', 1, 2005)");
query.exec("insert into albums values(3, '...The Great October Sound', 2, "
"2002)");
query.exec("insert into albums values(4, 'Stray Dogs', 2, 2003)");
query.exec("insert into albums values(5, "
"'One day you`ll dance for me, New York City', 2, 2004)");
query.exec("insert into albums values(6, 'Ompa Til Du D\xf8r', 3, 2001)");
query.exec("insert into albums values(7, 'Evig Pint', 3, 2002)");
query.exec("insert into albums values(8, 'Maestro', 3, 2005)");

return true;
}

#endif


ArtistsSqlModel.cpp

#include "ArtistsSqlModel.h";


ArtistsSqlModel::ArtistsSqlModel(QObject *parent) :
QSqlQueryModel(parent)
{
int idx = 0;
QHash<int, QByteArray> roleNames;
while( COLUMN_NAMES[idx]) {
roleNames[Qt::UserRole + idx + 1] = COLUMN_NAMES[idx];
idx++;
}
setRoleNames(roleNames);
refresh();
}


ArtistItemDelegate.qml

import Qt 4.7

Item {
id: delegate
width: delegate.ListView.view.width;
height: 30
clip: true
anchors.margins: 4

Row {
anchors.margins: 4
anchors.fill: parent
spacing: 4;

Text {
text: artist
width: 150
}

Text {
text: title
width: 300;
}

Text {
text: year
width: 50;
}
}
}


main.qml

import QtQuick 1.1

Rectangle {
width: 360
height: 360

ListView {
id: list_view1
clip: true
anchors.margins: 10
anchors.fill: parent
model: artistsModel
delegate: ArtistItemDelegate {}
}

}


main.cpp

#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include "ArtistsSqlModel.h"

QVariant ArtistsSqlModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if(role < Qt::UserRole)
{
value = QSqlQueryModel::data(index, role);
}
else
{
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
return value;
}

Q_DECL_EXPORT int main(int argc, char *argv[])
{
ArtistsSqlModel *artistsSqlModel = new ArtistsSqlModel( qApp);
QmlApplicationViewer viewer;
viewer.rootContext()->setContextProperty("artistsModel", artistsSqlModel);
viewer.setOrientation(QmlApplicationViewer::Screen OrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/SQLListView/main.qml"));
viewer.showExpanded();
}


I was getting this error

/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

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

Lykurg
15th February 2012, 06:57
Du you have QT += sqlin the *.pro file?

learningqt
15th February 2012, 09:41
thankz got it..:)
had to add some more lines to solve error: invalid use of incomplete type 'struct QDeclarativeContext'



#include <QDeclarativeContext>
#include <QKeyEvent>




QT +=sql
QT += core gui declarative


Thankz Lykurg

Lykurg
15th February 2012, 10:00
You are welcome.