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. Create File on Mac with QStandartPath failed
Forum Updated to NodeBB v4.3 + New Features

Create File on Mac with QStandartPath failed

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 2.1k Views 3 Watching
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi and welcome to the forums

    Try with
    StandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)

    Also the QApplication::processEvents(); is not needed at all and you should check
    the return value of open and use lastLZK.errorString(); to see what it says.

    1 Reply Last reply
    1
    • CForce-IT_AdminC Offline
      CForce-IT_AdminC Offline
      CForce-IT_Admin
      wrote on last edited by CForce-IT_Admin
      #3

      @mrjj said in Create File on Mac with QStandartPath failed:

      StandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)

      Thanks for that hint! -> (Also the QApplication::processEvents(); is not needed)

      For some reason it is working on QT engine.
      If I start the app after building and creating it with qmake it still does't work, can you tell me where I can find the file that should be created so I can check if it worked.
      I tried to clean all and recreating it but does not seem to have any effect.

      Thanks for that quick answer!

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi
        Just do
        qDebug() << "where:" << StandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
        to see where it points. I have no mac so cant test :)

        Docs says
        "~/Library/Application Support/<APPNAME>", "/Library/Application Support/<APPNAME>". "<APPDIR>/../Resources"
        so you better check what u get :)

        1 Reply Last reply
        2
        • CForce-IT_AdminC Offline
          CForce-IT_AdminC Offline
          CForce-IT_Admin
          wrote on last edited by CForce-IT_Admin
          #5

          I checked and I got an error.

          New code:

          QFile lastLZK (QStandardPaths::AppLocalDataLocation+"/cfLZK.activated");
          lastLZK.open(QIODevice::WriteOnly | QIODevice::Text);

              if (!lastLZK.error())
              {
          
              QString passKey;
              passKey = ui->lineEdit->text();
              QTextStream getKey(&lastLZK);
              getKey << passKey;
          
              }else {
              ui->lineEdit->setText("error");
              }
          

          There was no folder or file created in:
          /Library/Application Support/

          aha_1980A 1 Reply Last reply
          0
          • CForce-IT_AdminC CForce-IT_Admin

            I checked and I got an error.

            New code:

            QFile lastLZK (QStandardPaths::AppLocalDataLocation+"/cfLZK.activated");
            lastLZK.open(QIODevice::WriteOnly | QIODevice::Text);

                if (!lastLZK.error())
                {
            
                QString passKey;
                passKey = ui->lineEdit->text();
                QTextStream getKey(&lastLZK);
                getKey << passKey;
            
                }else {
                ui->lineEdit->setText("error");
                }
            

            There was no folder or file created in:
            /Library/Application Support/

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @CForce-IT_Admin what does https://doc.qt.io/qt-5/qiodevice.html#errorString say?

            Qt has to stay free or it will die.

            CForce-IT_AdminC 1 Reply Last reply
            1
            • aha_1980A aha_1980

              @CForce-IT_Admin what does https://doc.qt.io/qt-5/qiodevice.html#errorString say?

              CForce-IT_AdminC Offline
              CForce-IT_AdminC Offline
              CForce-IT_Admin
              wrote on last edited by
              #7

              @aha_1980

              syntax won't work (invalid argument type 'QString' to unary expression)


              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #8

                Hi
                Wait, you are using it wrong i think.
                QStandardPaths::AppLocalDataLocation is just an enum. not a string
                so you have to do
                QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);

                like

                QString FileName = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/cfLZK.activated";
                    QFile lastLZK (FileName);
                    if  (! lastLZK.open(QIODevice::WriteOnly | QIODevice::Text) ) {
                        ui->lineEdit->setText("file open error:"  + FileName + " errorStr:" + lastLZK.errorString()  );
                        return;
                    }
                
                    QString passKey;
                    passKey = ui->lineEdit->text();
                    QTextStream getKey(&lastLZK);
                    getKey << passKey;
                
                1 Reply Last reply
                0
                • CForce-IT_AdminC Offline
                  CForce-IT_AdminC Offline
                  CForce-IT_Admin
                  wrote on last edited by
                  #9

                  @mrjj said in Create File on Mac with QStandartPath failed:

                  QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/cfLZK.activated";

                  0_1562421976423_Screen Shot 2019-07-06 at 16.05.41.png

                  I tried that's what I get out of it.

                  mrjjM 1 Reply Last reply
                  0
                  • CForce-IT_AdminC CForce-IT_Admin

                    @mrjj said in Create File on Mac with QStandartPath failed:

                    QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/cfLZK.activated";

                    0_1562421976423_Screen Shot 2019-07-06 at 16.05.41.png

                    I tried that's what I get out of it.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @CForce-IT_Admin
                    well the syntax is slightly off, you try to assign string to QFile :)

                    Use QString and give that to QFile.

                    QString FileName = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/cfLZK.activated";
                    QFile lastLZK (FileName);

                    1 Reply Last reply
                    1
                    • CForce-IT_AdminC Offline
                      CForce-IT_AdminC Offline
                      CForce-IT_Admin
                      wrote on last edited by
                      #11

                      @mrjj said in Create File on Mac with QStandartPath failed:

                      if (! lastLZK.open(QIODevice::WriteOnly | QIODevice::Text) ) {
                      ui->lineEdit->setText("file open error:" + FileName + " errorStr:" + lastLZK.errorString() );
                      return

                      Sorry I rushed :) din't saw u had a second instance 'Filename'

                      output:

                      file open error:/Users/blackbeard/Library/Application Support/CForce-IT/CForce-IT Secure Data Manager/cfLZK.activated errorStr:No such file or directory


                      I might have to find a second solution. So I explain.

                      I need to save or store what is written in ui->lineEdit->Text()
                      and when the program starts to take the text and put back to ui->lineEdit->Text("stored text imput").

                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Hi
                        It says /Users/blackbeard/Library/Application Support/CForce-IT/CForce-IT Secure Data Manager/
                        does not exists. is this true ?

                        It can be as i had to make the folder myself on Windows but i have no idea how it works on Mac.

                        CForce-IT_AdminC 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          Hi
                          It says /Users/blackbeard/Library/Application Support/CForce-IT/CForce-IT Secure Data Manager/
                          does not exists. is this true ?

                          It can be as i had to make the folder myself on Windows but i have no idea how it works on Mac.

                          CForce-IT_AdminC Offline
                          CForce-IT_AdminC Offline
                          CForce-IT_Admin
                          wrote on last edited by CForce-IT_Admin
                          #13

                          @mrjj
                          yes it is true.

                          I think it might be some missing permissions to write.

                          I first tried to use QSettings but could't get the syntax to work. Maybe you could provide me with an example. I try to remember a QlineEdit input that I can bring back on program start. This should work Cross-Platform and would be the better solution.

                          Thanks for your help.

                          mrjjM 1 Reply Last reply
                          0
                          • CForce-IT_AdminC CForce-IT_Admin

                            @mrjj
                            yes it is true.

                            I think it might be some missing permissions to write.

                            I first tried to use QSettings but could't get the syntax to work. Maybe you could provide me with an example. I try to remember a QlineEdit input that I can bring back on program start. This should work Cross-Platform and would be the better solution.

                            Thanks for your help.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            @CForce-IT_Admin
                            Ok so its the same as on windows where the last folder of the location didn't exists for a new app.

                            Well you can use it like this. ( very basic sample)

                            void MainWindow::loadSettings()
                            {
                                QSettings settings("MyEditorName","myAppName");
                                QString sText = settings.value("text", "").toString();
                                ui->lineEdit->setText(sText);
                            }
                            
                            void MainWindow::saveSettings()
                            {
                                QSettings settings("MyEditorName","myAppName");
                                QString sText = ui->lineEdit->text();
                                settings.setValue("text", sText);
                            }
                            
                            1 Reply Last reply
                            3
                            • M Offline
                              M Offline
                              mpergand
                              wrote on last edited by mpergand
                              #15

                              I'm experienced similar issues on Mac !
                              I can't access to the writable location from the app contructor, i move all the code to exec() and here it works:

                              int WebApp::exec()
                              {
                                  setupFromPrefs();       // les valeurs sont fausses si appelé dans le constructeur !!!
                              //  Impossible d'accéder aux fichiers du dossier 'writable location' dans le constructeur !
                                  sHistoryManager=new HistoryManager(0);
                              

                              The comments in French say:

                              • The values return by QSettings are wrong
                              • no access to files in the writable location ( no error, the length of the file is zero)

                              I'm reading a file in the contructor of HistoryManager like that:

                              HistoryManager::HistoryManager(QObject* parent) : QObject(parent)
                              {
                                  QString path=QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
                              path+=HistoryFile;
                              
                              QFile hFile(path);
                              
                              if(hFile.exists())
                              	loadHistory(hFile);
                              
                              	createDialog();
                              
                              	EventCenter::instance()->addObserver(Event_AppPrefsDidChange,this);
                              }
                              

                              loadHistory reads zero bytes if processed to early in the startup Qt app sequence, move the code in exec() function solves the issue for me.

                              I have to mention that this kind of "synchronizing" issues are not unique to Qt, I see that on Mac too.
                              Even in your own code, there's a lot of circonstances where you need to pass in the eventloop before proceding forward.
                              That's why I have a simple macro for that:

                              #define InvokeLater(slot)  QMetaObject::invokeMethod(this, #slot,Qt::QueuedConnection)
                              

                              So, if you experienced strange issues as discribe here in the early state of your app, try to postpone the process, it may worth a try ;)

                              1 Reply Last reply
                              1
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                Hi,

                                Might be a silly question but did you check that the folder actually exists ?

                                The path is correct in the sense that it is where you should write but it might not exists yet.

                                So check the path returned, if the folder doesn't exist, create it first and then create your file in it.

                                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
                                2
                                • CForce-IT_AdminC Offline
                                  CForce-IT_AdminC Offline
                                  CForce-IT_Admin
                                  wrote on last edited by
                                  #17

                                  Thank u so much! Works like a charm.

                                  I had to adjust it a bit but works great here the code:

                                  void Encrypt::loadSettings()
                                  {
                                  QSettings settings("CForce_SDM","passKey");
                                  QString getkey = settings.value("text", "").toString();
                                  ui->lineEdit->setText(getkey);
                                  }

                                  void Encrypt::saveSettings()
                                  {
                                  QSettings settings("CForce_SDM","passKey");
                                  QString passKey = ui->lineEdit->text();
                                  //to clear key
                                  //QString passKey = "";
                                  settings.setValue("text", passKey);
                                  }


                                  This was exactly what I was looking for great job @mrjj

                                  aha_1980A 1 Reply Last reply
                                  0
                                  • CForce-IT_AdminC CForce-IT_Admin

                                    Thank u so much! Works like a charm.

                                    I had to adjust it a bit but works great here the code:

                                    void Encrypt::loadSettings()
                                    {
                                    QSettings settings("CForce_SDM","passKey");
                                    QString getkey = settings.value("text", "").toString();
                                    ui->lineEdit->setText(getkey);
                                    }

                                    void Encrypt::saveSettings()
                                    {
                                    QSettings settings("CForce_SDM","passKey");
                                    QString passKey = ui->lineEdit->text();
                                    //to clear key
                                    //QString passKey = "";
                                    settings.setValue("text", passKey);
                                    }


                                    This was exactly what I was looking for great job @mrjj

                                    aha_1980A Offline
                                    aha_1980A Offline
                                    aha_1980
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @CForce-IT_Admin

                                    //to clear key
                                    //QString passKey = "";

                                    No! An empty QString is just: QString passKey;

                                    Qt has to stay free or it will die.

                                    1 Reply Last reply
                                    1

                                    • Login

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