Congratulations to our 2022 Qt Champions!

Ressource file not found in Linux but works in Windows

  • Hi, I am using a QRessource to fetch an SQL to run. However it is getting the file in windows only.

    @QFile createTableSQLFile(":/sql/dbinstaller.sql");@

    Here's my meradatabase.h
    @class MeraDatabase
    static QSqlQuery exec(QString sql);
    static QSqlDatabase getDB();

    static QSqlDatabase init();
    static const QSqlDatabase db;
    static bool install(QSqlDatabase temp);


    @#include "meradatabase.h"
    #include <QtSql>
    #include "iostream"
    #include "string"
    #include <fstream>
    #include <QtDebug>
    #include <QSqlDatabase>

    using namespace std;

    const QSqlDatabase MeraDatabase::db = MeraDatabase::init();

    bool MeraDatabase::install(QSqlDatabase temp)
    qDebug() << "Installing..";

    // | QIODevice::Text);
    //QTextStream out(&installlock);
    //out << "locked";
    QString sql;
    QFile createTableSQLFile&#40;":/sql/dbinstaller.sql"&#41;;
    if(;&#41;{ // This is failing in Linux <<<<<<<<<<<<<<<<<<<
        //temp.exec&#40;"CREATE DATABASE fuzzycompostdb"&#41;;
        sql = createTableSQLFile.readAll(&#41;;
        qDebug(&#41; << ".. Creating tables..";
        qDebug(&#41; << temp.lastError();
        QFile addDefaultSubstrates(":/sql/defaultsubstrates.sql");
            sql = addDefaultSubstrates.readAll();
            qDebug(&#41; << ".. Adding Default Values.." <<sql;
            qDebug(&#41; << temp.lastError();
    return true;


    QSqlDatabase MeraDatabase::init()
    qDebug() << "Getting DB connection";
    QString dbName = "fuzzyDB";
    QString dbType = "QSQLITE";
    //QSqlDatabase db;
    QSqlDatabase temp;
    if(dbType == "QSQLITE"){
    temp = QSqlDatabase::addDatabase("QSQLITE");
    qDebug() << "sqlite temp..";
    temp.setDatabaseName(dbName + "db3");
    QFile installlock("installlock");

    QSqlQuery result = temp.exec&#40;"select * from substrate_type"&#41;;
    qDebug(&#41; << temp.lastError(&#41;;
    qDebug() << "name";
        qDebug() << "name" <<result.value("name");
    return temp;


    QSqlQuery MeraDatabase::exec(QString sql)
    return db.exec(sql);

    If i use the resource outside this class, it works fine on both Linux and windows. Dunno why my Linux Qt can't find the file.

    Best Regards

  • Moderators

    Make sure all the paths are correct. Windows uses different build directories by default, it can cause a lot of hard-to-debug bugs.

  • Please give your .pro file.
    The only reason is that resources does not be packed in your build.

  • Managed to get it to work. I put @ Q_INIT_RESOURCE(fcl);@ before using the files and it worked.

    Dunno why on linux and in static functions, Q_QINIT is required but works "out of the box" on Windows

    Thanks guys :)

Log in to reply