[solved] Opening file and saving path on Mac
-
wrote on 30 Dec 2013, 16:26 last edited by
Hello, I am using QtCreator 5.2.0 with Mavericks.
The following codes were written for Windows and work just fine with Windows. Not with Mac though!
- To browse for a path to a file:
[code]
// Button used to locate the Sometext.txt file
void OpenNavdata::on_butNavdataDialogBrowse_clicked()
{
navDataFilePath = QFileDialog::getOpenFileName(this, tr("Open Sometext.txt file"), "/", tr("Sometext.txt"));
ui->navDataDialogTextLine->setText(navDataFilePath);
}
[/code]In Mac, I get the Finder dialog box and I see the file, but it is greyed out and I cannot select it.
- The following code saves the path in a property (text) file:
[code]
// Button used to store the path of the file in the property file
void OpenNavdata::on_butNavdataDialogOK_clicked()
{
QString doc;
doc = "path=" + navDataFilePath + "\n";
QFile dataFile("configuration.property");if (dataFile.open(QFile::WriteOnly | QFile::Truncate)) { QTextStream out(&dataFile); out << doc; } dataFile.close(); close();
}
[/code]In Mac, it does not save any file at all.
Can someone help please? Much appreciated!
-
Hi,
Starting at the root ("/") path is generally a bad idea, you should rather start from the user home directory and on Mac AFAIK, the root directory is writable only by root and not the current user even if admin.
Where are you trying to write that file ? Are you sure you are having the rights to do that ? You also don't do anything if open fails like showing a dialog with the corresponding error
-
wrote on 30 Dec 2013, 23:44 last edited by
Hello, SGaist. Thank you for the response. First of all, I seem to have solved the issue of not being able to select the text file because it was greyed out. Instead of writing
[code]
navDataFilePath = QFileDialog::getOpenFileName(this, tr("Open Sometext.txt file"), "/", tr("Sometext.txt"));
[/code]
I wrote: "tr("*.txt")). It is weird but it worked.Problem 2 still exists. I was trying to write the property or cfg file in the same directory where the application is. Is it not how it works in Mac? OK, I tried to save it in Documents, but to no avail:
[code]
void OpenNavdata::on_butNavdataDialogOK_clicked()
{
QString doc;
doc = "navdata.path=" + navDataFilePath + "\n";
QFile dataFile("/Documents/application.cfg");if (dataFile.open(QFile::WriteOnly | QFile::Truncate)) { QTextStream out(&dataFile); out << doc; } dataFile.close(); close(); qDebug() << "DEBUG: " << navDataFilePath;
}
[/code]Not sure how to save a file on Mac.
Also, thanks for reminding of the error message if it does not open. I will deal with it once I figure out how to save the config file.
Happy New Year!
-
This parameter should contain a filter list, Sometext.txt doesn't correspond to a filter (you can look at getOpenFileName's documentation)
You should rather first deal with it, it will tell you why it can't open the file thus it will help you solve this problem
Same to you !
-
wrote on 31 Dec 2013, 20:00 last edited by
Looks like I manage to solve the save file issue by adding the whole path:
[code]
QFile dataFile("/Users/me/Documents/file.cfg");
[/code]This is not the solution I hoped to get since the path may be different for other users. Still do not understand why the heck the following does not work:
"/Documents/file.cfg"
Plus another problem that I will speak about in another thread.
Cheers
-
Simple: "/Documents" does not exist
Use "QStandardPaths":http://qt-project.org/doc/qt-5.0/qtcore/qstandardpaths.html to retrieve the user document folder, it will work cross platform. If it's a file related only to your application handling, then the DataLocation will be more suited
-
wrote on 31 Dec 2013, 21:15 last edited by
Thanks so much, again!
-
wrote on 1 Jan 2014, 21:00 last edited by
QStandardPaths did the work. Thanks a lot for the help!
1/8