Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] Opening file and saving path on Mac
Forum Update on Monday, May 27th 2025

[solved] Opening file and saving path on Mac

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 5.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    igorland
    wrote on 30 Dec 2013, 16:26 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 30 Dec 2013, 21:26 last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        igorland
        wrote on 30 Dec 2013, 23:44 last edited by
        #3

        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!

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 30 Dec 2013, 23:50 last edited by
          #4

          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 !

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • I Offline
            I Offline
            igorland
            wrote on 31 Dec 2013, 20:00 last edited by
            #5

            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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 31 Dec 2013, 20:18 last edited by
              #6

              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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • I Offline
                I Offline
                igorland
                wrote on 31 Dec 2013, 21:15 last edited by
                #7

                Thanks so much, again!

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  igorland
                  wrote on 1 Jan 2014, 21:00 last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  1/8

                  30 Dec 2013, 16:26

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved