PDA

View Full Version : Get user name



klipko
9th April 2008, 22:40
Does QT have a method to get the user name? I've found methods similar in QHostInfo but nothing directly to retrieve the logged in user name.

Thanks.

QT version: 4.3.2

wysota
9th April 2008, 23:32
No, it doesn't. What do you need it for? You should retrieve that using platform dependent means. You can use QDesktopServices::displayName(QDesktopServices::Ho meLocation) and extract the username from the result, but it might fail for non-standard setups.

klipko
10th April 2008, 00:16
Thanks wysota. The short, my app reserves and releases a motion controller (Ethernet based). But there are times when other users may need to use that controller, so I've implemented an un-reserved feature...bump the current user off. I wanted to display a messagebox if the user is trying to un-reserve the device if it's reserved from another user but not if the current user was the one who reserved it. (Confused?) The best option is to look at the user login name but I think just comparing the machine names maybe good enough. I know this feature can and maybe abused and it's been much debated but if the customer asks ($$$), I shall create.

What do you mean by "might fail for non-standard setups"?

jonman364
10th April 2008, 19:29
What about getenv("USERNAME")

gamitkumar
8th December 2008, 15:20
Hi
#include <stdlib.h>
U can use getenv("USER"); ///for MAc or Linux
and
getenv("USERNAME"); //for windows

Thanks
AMIT GUPTA
:)

araglin
19th December 2008, 13:07
also you could try call QProcess::systemEnvironment() and filter username from it's output.