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.

share|improve this question
1  
Thanks for posting my question :) – Daniel Holm May 7 '13 at 8:02
up vote 3 down vote accepted

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++.

share|improve this answer
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. – Daniel Holm May 7 '13 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 '13 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. – Daniel Holm May 7 '13 at 21:32

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"))
}
share|improve this answer
    
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, ^ – Cnorwood7641 Jan 29 '16 at 15:30
    
Whoops. I was missing an important part from that snippet: #include <QQmlEngine> – nocnokneo Jan 30 '16 at 22:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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