Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Save files in settings folder

Save files in settings folder

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 357 Views 1 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.
  • R Offline
    R Offline
    realroot
    wrote on last edited by
    #1
    Settings {
            category: "General"
            property alias x: root.x
            property alias y: root.y
    
        }
    

    Settings saves ~/.config/<OrganizationName>/<ApplicationName>.conf.

    How can I specify that path when using a function?
    Not manually but using something that works for all apps.

    L 1 Reply Last reply
    0
    • R realroot
      Settings {
              category: "General"
              property alias x: root.x
              property alias y: root.y
      
          }
      

      Settings saves ~/.config/<OrganizationName>/<ApplicationName>.conf.

      How can I specify that path when using a function?
      Not manually but using something that works for all apps.

      L Offline
      L Offline
      lemons
      wrote on last edited by
      #2

      @realroot you could do something like this:

      import Qt.labs.platform
      import Qt.labs.settings
      
      Settings {
          id: settings
          category: "General"
          // put it outside of settings to not store the configLocation as setting entry
          property string configLocation: StandardPaths.writableLocation(
                                                  StandardPaths.ConfigLocation)
          fileName: configLocation.replace("file://",
                                               "") + "/myOrganisation/myApp.conf"
      }
      

      Note:
      You can also pass the fileName as context property from main.cpp, or get it from any other C++ class.

      Settings {
          id: settings
          category: "General"
          fileName: myContextPropertySettingsLocation
          // fileName: backendClass.getSettingsLocation()
      }
      
      R 1 Reply Last reply
      0
      • L lemons

        @realroot you could do something like this:

        import Qt.labs.platform
        import Qt.labs.settings
        
        Settings {
            id: settings
            category: "General"
            // put it outside of settings to not store the configLocation as setting entry
            property string configLocation: StandardPaths.writableLocation(
                                                    StandardPaths.ConfigLocation)
            fileName: configLocation.replace("file://",
                                                 "") + "/myOrganisation/myApp.conf"
        }
        

        Note:
        You can also pass the fileName as context property from main.cpp, or get it from any other C++ class.

        Settings {
            id: settings
            category: "General"
            fileName: myContextPropertySettingsLocation
            // fileName: backendClass.getSettingsLocation()
        }
        
        R Offline
        R Offline
        realroot
        wrote on last edited by
        #3

        @lemons thanks.
        Instead of specyifing manually the OrganizationName and ApplicationName:

        fileName: configLocation.replace("file://", "") + "/myOrganization/myApp.cfg"
        

        If I want to save a file in the same folder can I specify it with some built-in var?
        For example:

        fileName: configLocation.replace("file://", "") + <OrganizationName> + "/" + <ApplicationName> + ".customFile"
        

        I can make some property string I ask in case there is something.

        L 1 Reply Last reply
        0
        • R realroot

          @lemons thanks.
          Instead of specyifing manually the OrganizationName and ApplicationName:

          fileName: configLocation.replace("file://", "") + "/myOrganization/myApp.cfg"
          

          If I want to save a file in the same folder can I specify it with some built-in var?
          For example:

          fileName: configLocation.replace("file://", "") + <OrganizationName> + "/" + <ApplicationName> + ".customFile"
          

          I can make some property string I ask in case there is something.

          L Offline
          L Offline
          lemons
          wrote on last edited by
          #4

          As mentioned in my previous answer, you can pass the information from anywhere to QML. You have to find the best solution for your application.

          e.g. Define them in your .pro file, and pass them all the way to QML:

          // .pro
          DEFINES += \
              ORGANIZATION_NAME="MyOrganizationName" \
              APP_NAME="MyAppName"
          
          // main.cpp
          QApplication app(argc, argv);
          
          QString orgName = QT_STRINGIFY(ORGANIZATION_NAME);
          QString appName = QT_STRINGIFY(APP_NAME);
          QString confLoc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
          
          QQmlApplicationEngine engine;
          
          // I prefer uppercase context properties, but this is not required
          engine.rootContext()->setContextProperty("ORGNAME", orgName);
          engine.rootContext()->setContextProperty("APPNAME", appName);
          engine.rootContext()->setContextProperty("CONFLOC", confLoc);
          // ...
          
          // .qml
          Settings {
              id: settings
              category: "General"
              fileName: CONFLOC + "/" + ORGNAME + "/" + APPNAME + ".conf"
              Component.onCompleted: {
                  console.debug("Config base path:", CONFLOC)
                  console.debug("Config file path:", settings.fileName)
              }
          }
          
          // you can use the context properties anywhere
          Text {
              anchors.centerIn: parent
              text: ORGNAME + ":" + APPNAME
          }
          
          1 Reply Last reply
          0

          • Login

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