Path of Application Bundle - Solved
-
Hi
I've added the file to the Resources by "Adding New Resource File".
I called my .qrc file MyResources.qrc
For the prefix, I put "files".I've tried:
QString stringPath = QCoreApplication::applicationFilePath();
stringPath = stringPath + "/../Resources/files/";and
QString stringPath = QCoreApplication::applicationFilePath();
stringPath = stringPath + "/Resources/files/";Both are not working for me.
Am I missing something?
Thank you in advance...
-
Yes you are,
The Resources from Qt are not the same thing as the Resources folder from an app bundle on OS X.
Read Qt's "resources documentation":http://qt-project.org/doc/qt-4.8/resources.html to have a better understanding of how to use them
-
Hi
Ok, I just read it....I'm starting to think that using the Qt resource system (.qrc) is not the way to go....
In there it says:
"The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable"Basically I have a function
swe_set_ephe_path(char * parameter)
that takes in a char * as a parameter.
The char * parameter should be the path to the directory where I store this file called "fixstars.cat".
I think it should be able to find the "fixstars.cat" file as it is and not as a binary file.I'm using a 3rd party API. To be able to use it properly for me, I should tell the API in which directory to find fixstars.cat, by passing in the path to the swe_set_ephe_path(char * parameter) function.
How can I achieve this in Qt?
Thank you very much for the help.
-
Something like ?
@QByteArray local8Bit = stringPath.toLocal8Bit();
swe_set_ephe_path(local8Bit.data());@ -
Hi
Actually, it is finding the right path to convert to char * that is my problem.
Here is my code:@QString stringPath = QCoreApplication::applicationFilePath();
stringPath = stringPath + "/Resources/files/"; qDebug() << stringPath; QByteArray ba = stringPath.toLocal8Bit(); char * c_str2 = ba.data(); swe_set_ephe_path(c_str2); double x[6]; char serr[365]; long ret_flag = swe_fixstar_ut("Spica", 300, SEFLG_NONUT, x, serr); qDebug() << ret_flag;
@
ret_flag prints out in the console as -1.
Meaning to say, i didn't enter the correct path to where fixstars.cat is.
I have added fixstars.cat to a .qrc file with "files" as the prefix. -
A .qrc resource is embedded in your binary not in your app bundle. Copy your fixstars.cat in your bundle or copy it from your resources to a temporary file and give the path to that temporary file to swe_set_ephe_path.
Anyway, you can't give a Qt resources path to a framework that doesn't support that, it won't be able to read it.
-
Hi
Ok. That is very good advice. Thank you.
I've been trying to go that route now.
I have been experimenting with the code to write to file:
@void MainWindow::write()
{
QFile file(QCoreApplication::applicationDirPath() + "/out.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "CANNOT OPEN";
return;
}qDebug() << "FILE OPENED"; QTextStream out(&file); out << "The magic number is: " << 49 << "\n"; file.flush(); file.close();
}@
I get the "FILE OPENED" string printed out to the console.
I would think that "out.txt" file would be created inside the folder where the executable for this app is. I go there and don't see any file.
I then search for out.txt in the Finder Window (I'm using a mac) and can't find any out.txt inside my comp.What am I doing incorrectly?
Howcome I can't write to that file.
I haven't created the out.txt, but based on some tutorials i've seen and read, i think the file should be created automatically by Qt if it doesn't exist....(I might be wrong, but that's what I think...)... -
I think you are currently getting to fast.
First thing to do is to check what file path you have i.e
@qDebug() << file.fileName();@
That will tell you where it is located.It should be created indeed. I don't know what is going wrong.
In any case, i would suggest you to start simple:
Put your fixstars.cat file in known location (i.e. Documents), try to load it from there using Qt. Once that work, move it in your bundle Resources/files subdirectory. Load it from there.Before embedding the file in you binary, answer these questions: Is it subject to change ? If so, how often ? Is it then really worth to put it in the binary rather than the bundle ?
Hope it helps
-
Hi
Thank you so much :)
I've managed to write the code for what I want:@
void MainWindow::read()
{
QFile mFile("/Users/bliss/Documents/fixstars.cat");if(!mFile.open(QFile::ReadOnly | QFile::Text)) { qDebug() << "could not open file for reading"; return; } QTextStream in(&mFile); QString mText = in.readAll(); qDebug() << mText; mFile.flush(); mFile.close();
}
void MainWindow::readFromResource()
{
QFile mFile(":/files/fixstars.cat");if(!mFile.open(QFile::ReadOnly | QFile::Text)) { qDebug() << "could not open file for reading"; return; } QTextStream in(&mFile); QString mText = in.readAll(); qDebug() << mText; qDebug() << "FROM readFromResource()"; mFile.flush(); mFile.close();
}
void MainWindow::readFromResourceWriteToApplicationDir()
{
QFile mFile(":/files/fixstars.cat");if(!mFile.open(QFile::ReadOnly | QFile::Text)) { qDebug() << "could not open file for reading"; return; } QTextStream in(&mFile); QString mText = in.readAll(); QFile file(QCoreApplication::applicationDirPath() + "/fixstars.cat"); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "CANNOT OPEN"; return; } qDebug() << "FILE OPENED"; QTextStream out(&file); out << mText; qDebug() << file.fileName(); file.flush(); file.close();
}@
-
You're welcome !
You don't need the QTextStream if you are reading the complete file anyway, QFile also has a readAll.
Don't forget to update the thread's title to solved so other forum users may know that a solution has been found :)