PDA

View Full Version : Get hostname from linux system



Raadush
26th April 2012, 07:41
Hi,
I have one question because Im definitely missing something. I need to save system $HOSTNAME into QString. When I run echo $HOSTNAME from terminal I get the result. But from Qt when I try getenv("HOSTNAME") I get nothing. I tried the same with USERNAME and everything works fine. Then I tried print QProcess::systemEnvironment() and HOSTNAME variable wasnt in the result. It seems like my linux system does not export HOSTNAME for other applications or what? Is there a way to get HOSTNAME easily into QString?

Thanks :rolleyes:

ChrisW67
26th April 2012, 08:59
Relying a environment variables is a bad idea anyway.

Open Qt Assistant (or the Help panel in Qt Creator), go to the index tab and type "hostname"... the manual is your friend.
Alternatively, since you started out with non-Qt functions, gethostname() (http://linux.die.net/man/2/gethostname)

Raadush
26th April 2012, 09:47
Thanks. I came across QHostInfo before, but when I tried to include it, compiler didnt find it. I think i must add path into gmake, but i dont know how :confused: If you can point me the right direction? gethostname works fine, thanks, but i'd like more using QHostInfo

Spitfire
27th April 2012, 09:45
// in your pro file
QT += network

PeterLLLLLL
13th July 2012, 07:47
It is very easy to ues it ....Creat a username...haha .....