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. QStandardPaths not available in QML?
Forum Updated to NodeBB v4.3 + New Features

QStandardPaths not available in QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqstandardpaths
16 Posts 5 Posters 7.8k Views 4 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 8 Dec 2015, 23:45 last edited by
    #5

    You can create a Q_ENUM and use it as parameter to your function. You can make it match the values you need from QStandardPaths so there's no translation needed just a cast.

    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 9 Dec 2015, 00:33
    0
    • S SGaist
      8 Dec 2015, 23:45

      You can create a Q_ENUM and use it as parameter to your function. You can make it match the values you need from QStandardPaths so there's no translation needed just a cast.

      ? Offline
      ? Offline
      A Former User
      wrote on 9 Dec 2015, 00:33 last edited by
      #6

      @SGaist

      thanks! got that to work (assuming I am doing the cast correctly with static_cast<QStandardPaths::StandardLocation>(location);).

      #ifndef UTILITYRP
      #define UTILITYRP
      
      #include <QObject>
      #include <QStandardPaths>
      
      class UtilityRP : public QObject
      {
          Q_OBJECT
      
      public:
          Q_INVOKABLE QStringList getPath(int location) {
              QStandardPaths::StandardLocation loc = static_cast<QStandardPaths::StandardLocation>(location);
              return QStandardPaths::standardLocations(loc);
          }
      };
      
      #endif // UTILITYRP
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 9 Dec 2015, 22:24 last edited by
        #7

        In this case, using an int is not clean. Your QML code won't make any sense (i.e. what is location 1 ?). You should really take the time to implement an enum for that. So that if any off the enum value changes, you won't have to update that part of your code and it will make it easier to read.

        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 9 Dec 2015, 22:54
        0
        • S SGaist
          9 Dec 2015, 22:24

          In this case, using an int is not clean. Your QML code won't make any sense (i.e. what is location 1 ?). You should really take the time to implement an enum for that. So that if any off the enum value changes, you won't have to update that part of your code and it will make it easier to read.

          ? Offline
          ? Offline
          A Former User
          wrote on 9 Dec 2015, 22:54 last edited by
          #8

          @SGaist

          Thanks. Yes, I realize using an arbitrary int kind of defeats the purpose of using an enum but I couldn't figure out how to do it on the QML side – the docs ( enumeration QML Basic Type ) are not very clear, nor how to expose the QStandardPaths:: StandardLocation to the QML.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 9 Dec 2015, 23:00 last edited by
            #9

            That's the QML doc, you should do it from C++. Add an enum to your class that maps the StandardLocation values to your own and declare it as a Q_ENUM.

            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 11 Dec 2015, 04:35
            0
            • S SGaist
              9 Dec 2015, 23:00

              That's the QML doc, you should do it from C++. Add an enum to your class that maps the StandardLocation values to your own and declare it as a Q_ENUM.

              ? Offline
              ? Offline
              A Former User
              wrote on 11 Dec 2015, 04:35 last edited by
              #10

              @SGaist

              Sorry, not following. Can you point me to an example that does that?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 11 Dec 2015, 22:47 last edited by
                #11
                #include <QApplication>
                #include <QQmlApplicationEngine>
                #include <QQmlContext>
                #include <QStandardPaths>
                #include <QQmlComponent>
                
                class UtilityRPAttachedEnums : public QObject
                {
                    Q_OBJECT
                public:
                    enum StandardLocation {
                        DesktopLocation       = QStandardPaths::DesktopLocation,
                        DocumentsLocation     = QStandardPaths::DocumentsLocation,
                        FontsLocation         = QStandardPaths::FontsLocation,
                        ApplicationsLocation  = QStandardPaths::ApplicationsLocation,
                        MusicLocation         = QStandardPaths::MusicLocation,
                        MoviesLocation        = QStandardPaths::MoviesLocation,
                        PicturesLocation      = QStandardPaths::PicturesLocation,
                        TempLocation          = QStandardPaths::TempLocation,
                        HomeLocation          = QStandardPaths::HomeLocation,
                        DataLocation          = QStandardPaths::DataLocation,
                        CacheLocation         = QStandardPaths::CacheLocation,
                        GenericCacheLocation  = QStandardPaths::GenericCacheLocation,
                        GenericDataLocation   = QStandardPaths::GenericDataLocation,
                        RuntimeLocation       = QStandardPaths::RuntimeLocation,
                        ConfigLocation        = QStandardPaths::ConfigLocation,
                        GenericConfigLocation = QStandardPaths::GenericConfigLocation,
                        DownloadLocation      = QStandardPaths::DownloadLocation,
                    };
                    Q_ENUMS(StandardLocation)
                
                public:
                    UtilityRPAttachedEnums(QObject *parent)
                        : QObject(parent)
                    {}
                    Q_INVOKABLE QStringList getPath(StandardLocation location) {
                        QStandardPaths::StandardLocation loc = static_cast<QStandardPaths::StandardLocation>(location);
                        return QStandardPaths::standardLocations(loc);
                    }
                };
                
                class UtilityRP : public QObject
                {
                    Q_OBJECT
                public:
                    static UtilityRPAttachedEnums *qmlAttachedProperties(QObject *object)
                    {
                        return new UtilityRPAttachedEnums(object);
                    }
                };
                
                QML_DECLARE_TYPEINFO(UtilityRP, QML_HAS_ATTACHED_PROPERTIES)
                
                int main(int argc, char *argv[])
                {
                    QApplication app(argc, argv);
                    qmlRegisterUncreatableType<UtilityRP>("org.test.qmlcomponents", 1, 0, "UtilityRP", "Static");
                
                    QQmlApplicationEngine engine;
                    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                
                    return app.exec();
                }
                
                #include "main.moc"
                

                main.qml

                import QtQuick 2.3
                import QtQuick.Controls 1.2
                
                import org.test.qmlcomponents 1.0
                
                ApplicationWindow {
                    visible: true
                    width: 640
                    height: 480
                    title: qsTr("Hello World")
                
                    menuBar: MenuBar {
                        Menu {
                            title: qsTr("File")
                            MenuItem {
                                text: qsTr("&Open")
                                onTriggered: console.log("Open action triggered");
                            }
                            MenuItem {
                                text: qsTr("Exit")
                                onTriggered: Qt.quit();
                            }
                        }
                    }
                
                    MouseArea {
                        id: mouseArea
                        anchors.fill: parent
                        Label {
                            id: label
                            text: qsTr("Click me")
                            anchors.centerIn: parent
                        }
                
                        onClicked: {
                            label.text = UtilityRP.getPath(UtilityRP.DesktopLocation);
                        }
                    }
                }
                

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

                ? benlauB 2 Replies Last reply 11 Dec 2015, 23:08
                2
                • S SGaist
                  11 Dec 2015, 22:47
                  #include <QApplication>
                  #include <QQmlApplicationEngine>
                  #include <QQmlContext>
                  #include <QStandardPaths>
                  #include <QQmlComponent>
                  
                  class UtilityRPAttachedEnums : public QObject
                  {
                      Q_OBJECT
                  public:
                      enum StandardLocation {
                          DesktopLocation       = QStandardPaths::DesktopLocation,
                          DocumentsLocation     = QStandardPaths::DocumentsLocation,
                          FontsLocation         = QStandardPaths::FontsLocation,
                          ApplicationsLocation  = QStandardPaths::ApplicationsLocation,
                          MusicLocation         = QStandardPaths::MusicLocation,
                          MoviesLocation        = QStandardPaths::MoviesLocation,
                          PicturesLocation      = QStandardPaths::PicturesLocation,
                          TempLocation          = QStandardPaths::TempLocation,
                          HomeLocation          = QStandardPaths::HomeLocation,
                          DataLocation          = QStandardPaths::DataLocation,
                          CacheLocation         = QStandardPaths::CacheLocation,
                          GenericCacheLocation  = QStandardPaths::GenericCacheLocation,
                          GenericDataLocation   = QStandardPaths::GenericDataLocation,
                          RuntimeLocation       = QStandardPaths::RuntimeLocation,
                          ConfigLocation        = QStandardPaths::ConfigLocation,
                          GenericConfigLocation = QStandardPaths::GenericConfigLocation,
                          DownloadLocation      = QStandardPaths::DownloadLocation,
                      };
                      Q_ENUMS(StandardLocation)
                  
                  public:
                      UtilityRPAttachedEnums(QObject *parent)
                          : QObject(parent)
                      {}
                      Q_INVOKABLE QStringList getPath(StandardLocation location) {
                          QStandardPaths::StandardLocation loc = static_cast<QStandardPaths::StandardLocation>(location);
                          return QStandardPaths::standardLocations(loc);
                      }
                  };
                  
                  class UtilityRP : public QObject
                  {
                      Q_OBJECT
                  public:
                      static UtilityRPAttachedEnums *qmlAttachedProperties(QObject *object)
                      {
                          return new UtilityRPAttachedEnums(object);
                      }
                  };
                  
                  QML_DECLARE_TYPEINFO(UtilityRP, QML_HAS_ATTACHED_PROPERTIES)
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication app(argc, argv);
                      qmlRegisterUncreatableType<UtilityRP>("org.test.qmlcomponents", 1, 0, "UtilityRP", "Static");
                  
                      QQmlApplicationEngine engine;
                      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                  
                      return app.exec();
                  }
                  
                  #include "main.moc"
                  

                  main.qml

                  import QtQuick 2.3
                  import QtQuick.Controls 1.2
                  
                  import org.test.qmlcomponents 1.0
                  
                  ApplicationWindow {
                      visible: true
                      width: 640
                      height: 480
                      title: qsTr("Hello World")
                  
                      menuBar: MenuBar {
                          Menu {
                              title: qsTr("File")
                              MenuItem {
                                  text: qsTr("&Open")
                                  onTriggered: console.log("Open action triggered");
                              }
                              MenuItem {
                                  text: qsTr("Exit")
                                  onTriggered: Qt.quit();
                              }
                          }
                      }
                  
                      MouseArea {
                          id: mouseArea
                          anchors.fill: parent
                          Label {
                              id: label
                              text: qsTr("Click me")
                              anchors.centerIn: parent
                          }
                  
                          onClicked: {
                              label.text = UtilityRP.getPath(UtilityRP.DesktopLocation);
                          }
                      }
                  }
                  
                  ? Offline
                  ? Offline
                  A Former User
                  wrote on 11 Dec 2015, 23:08 last edited by
                  #12

                  @SGaist

                  hey thanks! I'll be chewing on that for a while

                  1 Reply Last reply
                  0
                  • S SGaist
                    11 Dec 2015, 22:47
                    #include <QApplication>
                    #include <QQmlApplicationEngine>
                    #include <QQmlContext>
                    #include <QStandardPaths>
                    #include <QQmlComponent>
                    
                    class UtilityRPAttachedEnums : public QObject
                    {
                        Q_OBJECT
                    public:
                        enum StandardLocation {
                            DesktopLocation       = QStandardPaths::DesktopLocation,
                            DocumentsLocation     = QStandardPaths::DocumentsLocation,
                            FontsLocation         = QStandardPaths::FontsLocation,
                            ApplicationsLocation  = QStandardPaths::ApplicationsLocation,
                            MusicLocation         = QStandardPaths::MusicLocation,
                            MoviesLocation        = QStandardPaths::MoviesLocation,
                            PicturesLocation      = QStandardPaths::PicturesLocation,
                            TempLocation          = QStandardPaths::TempLocation,
                            HomeLocation          = QStandardPaths::HomeLocation,
                            DataLocation          = QStandardPaths::DataLocation,
                            CacheLocation         = QStandardPaths::CacheLocation,
                            GenericCacheLocation  = QStandardPaths::GenericCacheLocation,
                            GenericDataLocation   = QStandardPaths::GenericDataLocation,
                            RuntimeLocation       = QStandardPaths::RuntimeLocation,
                            ConfigLocation        = QStandardPaths::ConfigLocation,
                            GenericConfigLocation = QStandardPaths::GenericConfigLocation,
                            DownloadLocation      = QStandardPaths::DownloadLocation,
                        };
                        Q_ENUMS(StandardLocation)
                    
                    public:
                        UtilityRPAttachedEnums(QObject *parent)
                            : QObject(parent)
                        {}
                        Q_INVOKABLE QStringList getPath(StandardLocation location) {
                            QStandardPaths::StandardLocation loc = static_cast<QStandardPaths::StandardLocation>(location);
                            return QStandardPaths::standardLocations(loc);
                        }
                    };
                    
                    class UtilityRP : public QObject
                    {
                        Q_OBJECT
                    public:
                        static UtilityRPAttachedEnums *qmlAttachedProperties(QObject *object)
                        {
                            return new UtilityRPAttachedEnums(object);
                        }
                    };
                    
                    QML_DECLARE_TYPEINFO(UtilityRP, QML_HAS_ATTACHED_PROPERTIES)
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication app(argc, argv);
                        qmlRegisterUncreatableType<UtilityRP>("org.test.qmlcomponents", 1, 0, "UtilityRP", "Static");
                    
                        QQmlApplicationEngine engine;
                        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                    
                        return app.exec();
                    }
                    
                    #include "main.moc"
                    

                    main.qml

                    import QtQuick 2.3
                    import QtQuick.Controls 1.2
                    
                    import org.test.qmlcomponents 1.0
                    
                    ApplicationWindow {
                        visible: true
                        width: 640
                        height: 480
                        title: qsTr("Hello World")
                    
                        menuBar: MenuBar {
                            Menu {
                                title: qsTr("File")
                                MenuItem {
                                    text: qsTr("&Open")
                                    onTriggered: console.log("Open action triggered");
                                }
                                MenuItem {
                                    text: qsTr("Exit")
                                    onTriggered: Qt.quit();
                                }
                            }
                        }
                    
                        MouseArea {
                            id: mouseArea
                            anchors.fill: parent
                            Label {
                                id: label
                                text: qsTr("Click me")
                                anchors.centerIn: parent
                            }
                    
                            onClicked: {
                                label.text = UtilityRP.getPath(UtilityRP.DesktopLocation);
                            }
                        }
                    }
                    
                    benlauB Offline
                    benlauB Offline
                    benlau
                    Qt Champions 2016
                    wrote on 16 Dec 2015, 07:14 last edited by
                    #13

                    @SGaist You should start a library project in Github. :D

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 16 Dec 2015, 21:21 last edited by
                      #14

                      @benlau Do you mean a "nice to have helper functions/classes" library ? :)

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

                      benlauB 1 Reply Last reply 3 Mar 2016, 16:12
                      0
                      • S SGaist
                        16 Dec 2015, 21:21

                        @benlau Do you mean a "nice to have helper functions/classes" library ? :)

                        benlauB Offline
                        benlauB Offline
                        benlau
                        Qt Champions 2016
                        wrote on 3 Mar 2016, 16:12 last edited by
                        #15

                        @SGaist yes!

                        Something like that:

                        quickcross/qcstandardpaths.h at master · benlau/quickcross

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          Markus Goetz53
                          wrote on 23 Feb 2018, 16:17 last edited by
                          #16
                          import Qt.labs.platform 1.0
                          

                          https://doc.qt.io/qt-5.10/qml-qt-labs-platform-standardpaths.html

                          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