[solved] Opening file and saving path on Mac



  • 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!

    1. 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.

    1. 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!


  • Lifetime Qt Champion

    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



  • 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!


  • Lifetime Qt Champion

    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 !



  • 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


  • Lifetime Qt Champion

    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



  • Thanks so much, again!



  • QStandardPaths did the work. Thanks a lot for the help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.