Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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
    {
    public:
    static QSqlQuery exec(QString sql);
    static QSqlDatabase getDB();

    protected:
    static QSqlDatabase init();
    private:
    static const QSqlDatabase db;
    static bool install(QSqlDatabase temp);
    };@

    meradatabase.cpp

    @#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)
    {
    temp.open();
    qDebug() << "Installing..";

    //installlock.open(QIODevice::WriteOnly | QIODevice::Text);
    //QTextStream out(&installlock);
    //out << "locked";
    QString sql;
    
    
    QFile createTableSQLFile&#40;":/sql/dbinstaller.sql"&#41;;
    if(createTableSQLFile.open(QIODevice::ReadOnly&#41;&#41;{ // This is failing in Linux <<<<<<<<<<<<<<<<<<<
        
        //temp.exec&#40;"CREATE DATABASE fuzzycompostdb"&#41;;
        sql = createTableSQLFile.readAll(&#41;;
        temp.exec(sql&#41;;
        qDebug(&#41; << ".. Creating tables..";
        qDebug(&#41; << temp.lastError();
    
        QFile addDefaultSubstrates(":/sql/defaultsubstrates.sql");
        if(addDefaultSubstrates.open(QIODevice::ReadOnly)){
            sql = addDefaultSubstrates.readAll();
            temp.exec&#40;sql&#41;;
            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.addDatabase("QSQLITE");
    temp.setDatabaseName(dbName + "db3");
    QFile installlock("installlock");
    if(!installlock.exists()){
    install(temp);
    }
    }

    QSqlQuery result = temp.exec&#40;"select * from substrate_type"&#41;;
    qDebug(&#41; << temp.lastError(&#41;;
    qDebug() << "name";
    while(result.next()){
        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
    Nayar


  • 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