tags:

views:

496

answers:

3

Hi, I am very new to using QT4. I am trying to use sql in an already working application. However, when I try to compile I get this error: "‘QSqlDatabase’ was not declared in this scope."

Here is the relevent code:

#include <QtSql>
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");

 db.setHostName("bigblue");
 db.setDatabaseName("flightdb");
 db.setUserName("acarlson");
 db.setPassword("1uTbSbAs");
 bool ok = db.open();

I have added QT += sql to my .pro file.

The code above it literally copied from here:http://qt.nokia.com/doc/4.5/qtsql.html

Any idea what I am doing wrong?

A: 

I think you just need a

#include <QSqlDatabase>

as this is not automatically included by QtSql.

Dirk Eddelbuettel
Just tried it, here are the new errors.error: QtSql: No such file or directorysrc/mainwindowimpl.cpp:9:24: error: QSqlDatabase: No such file or directorysrc/mainwindowimpl.cpp: In member function ‘void MainWindowImpl::not_done()’:src/mainwindowimpl.cpp:58: error: ‘QSqlDatabase’ was not declared in this scopesrc/mainwindowimpl.cpp:58: error: expected `;' before ‘db’src/mainwindowimpl.cpp:59: error: ‘db’ was not declared in this scopesrc/mainwindowimpl.cpp:63: warning: unused variable ‘ok’
Piratebill
QtSql includes QtSqlDatabase
PiedPiper
"I'm right, you're wrong" doesn't tend to make be trust you any more than the other person. Any supporting evidence for your statement?
Dems
@Dems /usr/include/qt4/QtSql/QtSql contains the line:#include "qsqldatabase.h"Is that enough evidence?
PiedPiper
A: 

The compiler can't find the QtSql header and therefore doesn't know about QSqlDatabase. The header is in the QtSql subdirectory, so maybe you need to explicitly specify this in the include line:

#include <QtSql/QtSql>

Alternatively you have to make sure that the QtSql directory is in the include file search path of your compiler.

sth
That just made it go from 3 errors, to 10. Do you want me to post them all?
Piratebill
Usually the first error is the most significant one... What does the first error message say?
sth
+3  A: 

Ok, the problem was with my .pro file. This is the file that gave me the error:

TEMPLATE = app
QT += sql
QT = gui core
CONFIG += qt debug warn_on console
DESTDIR = bin
OBJECTS_DIR = build
MOC_DIR = build
UI_DIR = build
FORMS = ui/mainwindow.ui ui/dialog.ui ui/dialog_con.ui ui/add_ingredient.ui
HEADERS = src/mainwindowimpl.h \
src/dialogimpl.h \
src/utils.h \
SOURCES = src/mainwindowimpl.cpp \
src/main.cpp \
src/dialogimpl.cpp \
src/utils.cpp \

The problem was that QT=gui core was overriding my QT+=sql. by moving the QT+=sql to bo right above HEADERS, it compiled just fine. Thanks for the help.

Piratebill
I think that usually you shouldn't assign (with = )variables on a qmake file, but is better to add or remove values from them (with += or -=), or you can have some side effects as it happened to you.
Federico
Piratebill: If this was the solution, you should also mark it as accepted.
Caleb Huitt - cjhuitt