3

I need to know how to get the user name and/or home directory of the users. I've googled around for a while but can only find the variables for C++ or BASH.

How do I get the user name or home directory? I'm writing in QML.

1
  • 2
    Thanks for posting my question :) May 7, 2013 at 8:02

3 Answers 3

8

This is how I implemented it:

QmlEnvironmentVariable.h

#ifndef QMLENVIRONMENTVARIABLE_H
#define QMLENVIRONMENTVARIABLE_H

#include <QObject>

class QQmlEngine;
class QJSEngine;

class QmlEnvironmentVariable : public QObject
{
   Q_OBJECT
public:    
   Q_INVOKABLE static QString value(const QString &name);
   Q_INVOKABLE static void setValue(const QString &name, const QString &value);
   Q_INVOKABLE static void unset(const QString &name);
};

// Define the singleton type provider function (callback).
QObject *qmlenvironmentvariable_singletontype_provider(QQmlEngine *, QJSEngine *);

#endif // QMLENVIRONMENTVARIABLE_H

QmlEnvironmentVariable.cpp

#include "QmlEnvironmentVariable.h"
#include <stdlib.h>

QString QmlEnvironmentVariable::value(const QString& name)
{
   return qgetenv(qPrintable(name));
}

void QmlEnvironmentVariable::setValue(const QString& name, const QString &value)
{
   qputenv(qPrintable(name), value.toLocal8Bit());
}

void QmlEnvironmentVariable::unset(const QString& name)
{
   qunsetenv(qPrintable(name));
}

QObject *qmlenvironmentvariable_singletontype_provider(QQmlEngine *, QJSEngine *)
{
   return new QmlEnvironmentVariable();
}

Then in main() add a call to qmlRegisterSingletonType (or in your re-implemented QQmlExtensionPlugin::registerTypes() method if you're creating a plugin):

#include "QmlEnvironmentVariable.h"
#include <QQmlEngine>
// ...
qmlRegisterSingletonType<QmlEnvironmentVariable>("MyModule", 1, 0,
    "EnvironmentVariable", qmlenvironmentvariable_singletontype_provider);

Finally, use it in QML like so:

import MyModule 1.0
Item {
  Component.onCompleted: console.log("My home directory: " + EnvironmentVariable.value("HOME"))
}
2
  • I don't know C++ so I can't figure you how to do the third step. I pasted it into main.cpp and I got: /home/collin/liri-dev/liri-player/src/main.cpp:12: error: 'qmlRegisterSingletonType' does not name a type qmlRegisterSingletonType<QmlEnvironmentVariable>("MyModule", 1, 0, ^ Jan 29, 2016 at 15:30
  • Whoops. I was missing an important part from that snippet: #include <QQmlEngine>
    – nocnokneo
    Jan 30, 2016 at 22:33
3

You would have to get the Username in C++ and then exchange that data from C++ to qml.

Read here how to exchange data between qml and C++.

3
  • 1
    I was afraid of that. I don't know C++, nor am I interested in learning it.. I just dont get these header files, and then a seperate source file. ANd then to compile stuff. I'd be happy to get some help in this matter. May 7, 2013 at 18:50
  • 2
    @DanielHolm qml might be easy to use and provide rapid development capability, but has its constraints. I would suggest you to learn C++ as well. May be just the basics. The combination, is DEADLY. Try reading this amazon.com/Primer-5th-Stanley-B-Lippman/dp/0321714113 or amazon.com/An-Introduction-Design-Patterns-Qt/dp/0131879057 if you want to go through the Qt way directly. Its not that difficult. Try, fall, come to the community, people will surely help.
    – Amit Tomar
    May 7, 2013 at 18:55
  • Thanks a lot, Amit. I will take a look. Also I've asked in the Ubuntu Touch mail list for some help until I'm able to make it myself. I'm currently studying political environmental science, that's my QML is so much easier to get going then C++ - at the moment. But, as I was sayin', I will take a look. May 7, 2013 at 21:32
0

My solution was like this:

1.) create config.h file with the Config class:

#ifndef CONFIG_H
#define CONFIG_H

#include <QString>
#include <QObject>
#include <QStandardPaths>

class Config : public QObject
{
    Q_OBJECT

public:
    explicit Config(QObject *parent = nullptr) {}

    Q_INVOKABLE QString getHome() {
        return QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first();
    }
};

#endif // CONFIG_H

2.) set the setContextProperty(...) in your main.cpp

int main(int argc, char *argv[]) {
    ...
    Config config;
    viewer.rootContext()->setContextProperty("config", config);
}

Then you can simply call config.getHome() in your qml-js file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.