Starting with Qt Creator - Sql problem
-
Hi everyone,
as a brand new user of QT creator I stumble into some problems trying to follow a simple tutorial to aid my learning curve. I want to create a simple database.I created a new project and selected Application -> Widgets Application
in my .pro file I added QT += sql
in my main.cpp I added #include <QtSql/QSqlDatabase>Now I try to create a database using
QSqlDatabase db; db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("/home/myspace/testdb.sqlite");
Now I was expecting it to recognize QSqlDatabase, but it only recognizes QT_SQL_LIB
Did I go wrong installing, configuring or is the tutorial I am trying to follow outdated?
I did try running clean and qmake after adding sql to my .pro file.
-
@Seabird said in Starting with Qt Creator - Sql problem:
Now I was expecting it to recognize QSqlDatabase, but it only recognizes QT_SQL_LIB
What does this mean? Do you get a compiler error?
-
@Christian-Ehrlicher it gives me unknow type name 'QSqlDatabase'
and in the compiler it kicks a error undifened reference to QSqlDatabase::QSqlDatabase()(sorry for slow responds, but am only allowed to reply every 10 minutes)
-
Ok, the video tutorial I am trying to follow is slightly outdated (2017) but the goal is the same:
This is the tutorial I am trying to follow: youtube tutorial
My sqltest.pro
QT -= gui QT += core sql CONFIG += c++11 console CONFIG -= app_bundle DEFINES += QT_DEPRECATED_WARNINGS deprecated before Qt 6.0.0 SOURCES += \ main.cpp # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
and my main.cpp
#include <QCoreApplication> #include <QSqlDatabase> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QSqlDatabase db; db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("/home/jacco/testdb.sqlite"); return a.exec(); }
I cleaned build folder, ran qmake, build etc.
Three issues reported:
- list item unknown type name 'QCoreApplication'
- list item unknown type name 'QSqlDatabase'
- list item use of undeclared identifier 'QSqlDatabase'
-
@Seabird said in Starting with Qt Creator - Sql problem:
Three issues reported
To make things clear: do you get this errors in QtCreator inside editor, or do you get them when you compile your project?
-
@Seabird said in Starting with Qt Creator - Sql problem:
Any reason it does not recognize the QSqlDatabase statements?
Yes, known issue with clang code model. You can disable it, go to: "Help/About Plugins..." and disable ClangCodeModel plug-in.