QStandardPaths not available in QML?
-
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
-
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.
-
Thanks. Yes, I realize using an arbitrary
int
kind of defeats the purpose of using anenum
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. -
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.
-
#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); } } }
-
-
import Qt.labs.platform 1.0
https://doc.qt.io/qt-5.10/qml-qt-labs-platform-standardpaths.html