sqliteqt5qdir

Qt - Auto detection of a file in application folder


I have a sqlite database of my Qt C++ application. Suppose I located it on my default build/release folder and I also placed the database file on that same folder. I have login.h and login.cpp. I want that the application may auto detect the database and open it. I will only provide the name of database (Ex: mydb.sqlite).

Database.addDatabase("QSQLITE");
Database.setDatabaseName("I will only provide database file name here.like: mydb.sqlite");

I want that the rest of the directory should automatically detected and the database connection works perfectly. Suppose, I have the sqlite file in: C:/Qt/build-myapp-mingw-32/mydb.sqlite. I am taking a string variable called path. And I want that, the application automatically detect the whole path. And open the database connection.


Solution

  • You do not have to give the complete path to database to make it works. You can refer to the path from your build dir or maybe use :

    QCoreApplication::applicationDirPath();
    

    To get the path of your app and then navigate through your directories with .. ?