diff --git a/src/corelib/io/qstandardpaths.cpp b/src/corelib/io/qstandardpaths.cpp
index b41aee5..d7412a7 100644
--- a/src/corelib/io/qstandardpaths.cpp
+++ b/src/corelib/io/qstandardpaths.cpp
@@ -366,6 +366,24 @@ static bool existsAsSpecified(const QString &path, QStandardPaths::LocateOptions
 }
 
 /*!
+    This function enables or disables XDG locations on platforms where Qt can be configured
+    to use this kind of locations instead of locations following OS guidelines. Currently
+    this concerns only Mac OS X //and Cygwin?//
+ */
+static void QStandardPaths::setXDGLocationsEnabled(bool xdgMode);
+{
+    usingXDGLocations = xdgMode;
+}
+
+/*!
+    This function returns true when XDG locations are used.
+ */
+static bool QStandardPaths::isXDGLocationsEnabled()
+{
+    return usingXDGLocations;
+}
+
+/*!
    Tries to find a file or directory called \a fileName in the standard locations
    for \a type.
 
diff --git a/src/corelib/io/qstandardpaths.h b/src/corelib/io/qstandardpaths.h
index e305450..8f9fb23 100644
--- a/src/corelib/io/qstandardpaths.h
+++ b/src/corelib/io/qstandardpaths.h
@@ -67,6 +67,9 @@ public:
         AppLocalDataLocation = DataLocation
     };
 
+    static void setXDGLocationsEnabled(bool xdgMode);
+    static bool isXDGLocationsEnabled();
+
     static QString writableLocation(StandardLocation type);
     static QStringList standardLocations(StandardLocation type);
 
@@ -94,6 +97,15 @@ private:
     // prevent construction
     QStandardPaths();
     ~QStandardPaths();
+    static bool usingXDGLocations;
+};
+
+struct Q_CORE_EXPORT QStandardPathsConfiguration
+{
+    QStandardPathsConfiguration(bool useXDGLocations)
+    {
+        QStandardPaths::setXDGLocationsEnabled(useXDGLocations);
+    }
 };
 
 #endif // QT_NO_STANDARDPATHS
diff --git a/src/corelib/io/qstandardpaths_android.cpp b/src/corelib/io/qstandardpaths_android.cpp
index 958b4ea..fdc2634 100644
--- a/src/corelib/io/qstandardpaths_android.cpp
+++ b/src/corelib/io/qstandardpaths_android.cpp
@@ -215,6 +215,8 @@ static QString getFilesDir()
     return (path  = QDir::homePath());
 }
 
+bool QStandardPaths::usingXDGLocations = false;
+
 QString QStandardPaths::writableLocation(StandardLocation type)
 {
     switch (type) {
diff --git a/src/corelib/io/qstandardpaths_blackberry.cpp b/src/corelib/io/qstandardpaths_blackberry.cpp
index 02d7c11..d28b320 100644
--- a/src/corelib/io/qstandardpaths_blackberry.cpp
+++ b/src/corelib/io/qstandardpaths_blackberry.cpp
@@ -47,6 +47,8 @@ static QString testModeInsert() {
         return QStringLiteral("");
 }
 
+bool QStandardPaths::usingXDGLocations = false;
+
 QString QStandardPaths::writableLocation(StandardLocation type)
 {
     QDir sharedDir = QDir::home();
diff --git a/src/corelib/io/qstandardpaths_ios.mm b/src/corelib/io/qstandardpaths_ios.mm
index 9b500f4..90a988b 100644
--- a/src/corelib/io/qstandardpaths_ios.mm
+++ b/src/corelib/io/qstandardpaths_ios.mm
@@ -58,6 +58,8 @@ static QString bundlePath()
     return QString::fromNSString([[NSBundle mainBundle] bundlePath]);
 }
 
+bool QStandardPaths::usingXDGLocations = false;
+
 QString QStandardPaths::writableLocation(StandardLocation type)
 {
     QString location;
diff --git a/src/corelib/io/qstandardpaths_mac.mm b/src/corelib/io/qstandardpaths_mac.mm
index 13b8646..76f5869 100644
--- a/src/corelib/io/qstandardpaths_mac.mm
+++ b/src/corelib/io/qstandardpaths_mac.mm
@@ -38,6 +38,7 @@
 
 #ifndef QT_BOOTSTRAPPED
 #include <qcoreapplication.h>
+#include <qlibraryinfo.h>
 #endif
 
 #include <CoreFoundation/CoreFoundation.h>
@@ -134,6 +135,8 @@ static QString macLocation(QStandardPaths::StandardLocation type, short domain)
     return path;
 }
 
+bool QStandardPaths::usingXDGLocations = false;
+
 QString QStandardPaths::writableLocation(StandardLocation type)
 {
     if (isTestModeEnabled()) {
@@ -178,17 +181,92 @@ QString QStandardPaths::writableLocation(StandardLocation type)
     }
 }
 
+static void normaliseDirs(QStringList &dirs)
+{
+    // Normalise paths, skip relative paths
+    QMutableListIterator<QString> it(dirs);
+    while (it.hasNext()) {
+        const QString dir = it.next();
+        if (!dir.startsWith(QLatin1Char('/')))
+            it.remove();
+        else
+            it.setValue(QDir::cleanPath(dir));
+    }
+
+    // Remove duplicates from the list, there's no use for duplicated
+    // paths in XDG_CONFIG_DIRS - if it's not found in the given
+    // directory the first time, it won't be there the second time.
+    // Plus duplicate paths causes problems for example for mimetypes,
+    // where duplicate paths here lead to duplicated mime types returned
+    // for a file, eg "text/plain,text/plain" instead of "text/plain"
+    dirs.removeDuplicates();
+}
+
+static QStringList xdgConfigDirs()
+{
+    QStringList dirs;
+    // http://standards.freedesktop.org/basedir-spec/latest/
+    QString xdgConfigDirsEnv = QFile::decodeName(qgetenv("XDG_CONFIG_DIRS"));
+    if (xdgConfigDirsEnv.isEmpty()) {
+#ifndef QT_BOOTSTRAPPED
+        dirs.append(QLibraryInfo::location(QLibraryInfo::PrefixPath) + QString::fromLatin1("/config"));
+#endif
+    } else {
+        dirs = xdgConfigDirsEnv.split(QLatin1Char(':'), QString::SkipEmptyParts);
+
+        normaliseDirs(dirs);
+    }
+    return dirs;
+}
+
+static QStringList xdgDataDirs()
+{
+    QStringList dirs;
+    // http://standards.freedesktop.org/basedir-spec/latest/
+    QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
+    if (xdgDataDirsEnv.isEmpty()) {
+#ifndef QT_BOOTSTRAPPED
+        dirs.append(QLibraryInfo::location(QLibraryInfo::PrefixPath) + QString::fromLatin1("/share"));
+#endif
+    } else {
+        dirs = xdgDataDirsEnv.split(QLatin1Char(':'), QString::SkipEmptyParts);
+
+        normaliseDirs(dirs);
+    }
+    return dirs;
+}
+
 QStringList QStandardPaths::standardLocations(StandardLocation type)
 {
     QStringList dirs;
 
+    if (usingXDGLocations) {
+        if (type == GenericDataLocation) {
+            dirs.append(xdgDataDirs());
+        }
+    }
+
     if (type == GenericDataLocation || type == AppDataLocation || type == AppLocalDataLocation || type == GenericCacheLocation || type == CacheLocation) {
         const QString path = macLocation(type, kOnAppropriateDisk);
         if (!path.isEmpty())
             dirs.append(path);
     }
 
+    if (usingXDGLocations) {
+        if (type == GenericConfigLocation || type == ConfigLocation) {
+            dirs.append(xdgConfigDirs());
+        }
+    }
+
     if (type == AppDataLocation || type == AppLocalDataLocation) {
+        if (usingXDGLocations) {
+            QStringList xdgDirs = xdgDataDirs();
+            for (int i = 0; i < xdgDirs.count(); ++i) {
+                appendOrganizationAndApp(xdgDirs[i]);
+            }
+            dirs.append(xdgDirs);
+        }
+
         CFBundleRef mainBundle = CFBundleGetMainBundle();
         if (mainBundle) {
             CFURLRef bundleUrl = CFBundleCopyBundleURL(mainBundle);
diff --git a/src/corelib/io/qstandardpaths_unix.cpp b/src/corelib/io/qstandardpaths_unix.cpp
index 2ad6dfa..8d25055 100644
--- a/src/corelib/io/qstandardpaths_unix.cpp
+++ b/src/corelib/io/qstandardpaths_unix.cpp
@@ -62,6 +62,8 @@ static void appendOrganizationAndApp(QString &path)
 #endif
 }
 
+bool QStandardPaths::usingXDGLocations = true;
+
 QString QStandardPaths::writableLocation(StandardLocation type)
 {
     switch (type) {
diff --git a/src/corelib/io/qstandardpaths_win.cpp b/src/corelib/io/qstandardpaths_win.cpp
index 5e56db3..d25c32b 100644
--- a/src/corelib/io/qstandardpaths_win.cpp
+++ b/src/corelib/io/qstandardpaths_win.cpp
@@ -85,6 +85,8 @@ static inline int clsidForAppDataLocation(QStandardPaths::StandardLocation type)
 #endif
 }
 
+bool QStandardPaths::usingXDGLocations = false;
+
 QString QStandardPaths::writableLocation(StandardLocation type)
 {
     QString result;
diff --git a/src/corelib/io/qstandardpaths_winrt.cpp b/src/corelib/io/qstandardpaths_winrt.cpp
index 5c09b48..73b516f 100644
--- a/src/corelib/io/qstandardpaths_winrt.cpp
+++ b/src/corelib/io/qstandardpaths_winrt.cpp
@@ -60,6 +60,8 @@ static QString convertCharArray(const wchar_t *path)
     return QDir::fromNativeSeparators(QString::fromWCharArray(path));
 }
 
+bool QStandardPaths::usingXDGLocations = false;
+
 QString QStandardPaths::writableLocation(StandardLocation type)
 {
     QString result;
