EMJB
Posts: 111
Joined: Wed Jun 12, 2013 8:29 am

QT QHostInfo

Mon Jan 29, 2018 5:48 pm

I would like to use the QHostInfo class in my Pi QT application, but I get "QHostInfo: No such file or directory" error against the "#include <QHostInfo> " statement in the ".cpp" file. However "class QHostInfo;" in the ".h" file does not generate an error. I think I am using the latest version of QT for the Pi (5.3.2). Can anyone explain what I am doing wrong?

EMJB

Daniel Gessel
Posts: 21
Joined: Sun Dec 03, 2017 1:47 am

Re: QT QHostInfo

Mon Jan 29, 2018 7:22 pm

Does #include<QHostinfo.h> (instead of #include<QHostinfo>) help?

EMJB
Posts: 111
Joined: Wed Jun 12, 2013 8:29 am

Re: QT QHostInfo

Tue Jan 30, 2018 8:04 am

Daniel Gessel wrote:
Mon Jan 29, 2018 7:22 pm
Does #include<QHostinfo.h> (instead of #include<QHostinfo>) help?
Thanks for the suggestion, but unfortunately it doesn't seem to help.

EMJB

Daniel Gessel
Posts: 21
Joined: Sun Dec 03, 2017 1:47 am

Re: QT QHostInfo

Tue Jan 30, 2018 3:35 pm

Then you probably need a -I<path> option on your gcc command line. Do you know where the QHostinfo file is on your system? The path to that directory is what you’d use with the -I option.

EMJB
Posts: 111
Joined: Wed Jun 12, 2013 8:29 am

Re: QT QHostInfo

Wed Jan 31, 2018 8:45 am

Daniel Gessel wrote:
Tue Jan 30, 2018 3:35 pm
Then you probably need a -I<path> option on your gcc command line. Do you know where the QHostinfo file is on your system? The path to that directory is what you’d use with the -I option.
Thanks - that has helped me move forward, but not there yet. I found both "QHostInfo" and "qhostinfo.h" in "/usr/include/arm-linux-gnueabihf/qt5/QtNetwork/", with the former simply calling up the latter. When I add either to the include list wiith the full path, I no longer get an error message with a line number against "HostName = QHostInfo::localHostName();", but instead get one as follows:
In function 'MainWindow ::MainWindow(QWidget*): mainwindow.o
undefined reference to 'QHostInfo:localHostName()' mainwindow.cpp
collect2: error: Id returned 1 exit status
which looks as if it can't find the code for the localHostName function, so your -l option looks hopeful. However I cannot find where in the QT options I should insert this - is it the Platform Linker flags?

User avatar
topguy
Posts: 4427
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: QT QHostInfo

Wed Jan 31, 2018 10:32 am

How are your make system for you application set up ? Standard make, cmake or qmake ?
Using QtCreator or some other IDE ?

Since you now used a class from QtNetwork I will guess that you also has to link with QtNetwrok by adding "-lQt5Network" to your linker options.

EMJB
Posts: 111
Joined: Wed Jun 12, 2013 8:29 am

Re: QT QHostInfo

Wed Jan 31, 2018 11:11 am

topguy wrote:
Wed Jan 31, 2018 10:32 am
How are your make system for you application set up ? Standard make, cmake or qmake ?
Using QtCreator or some other IDE ?
I'm using the default Pi QT5 setup which I think uses qmake, with QtCreator.
topguy wrote:
Wed Jan 31, 2018 10:32 am
Since you now used a class from QtNetwork I will guess that you also has to link with QtNetwrok by adding "-lQt5Network" to your linker options.
Excuse my ignorance, but where are the linker options specified?

EMJB

EMJB
Posts: 111
Joined: Wed Jun 12, 2013 8:29 am

Re: QT QHostInfo

Wed Jan 31, 2018 11:28 am

Oops - just remembered the ".pro" file - I was looking for somewhere generic to all applications - sorry for that. It does seem rather illogical to include the network classes, but not to link to them by default.

I have added "-lQt5Network" to the "LIBS +=" statement, and it now compiles and runs, but am now looking at why I get a blank name.

Many thanks for your help, topguy & Daniel.

EMJB

EMJB
Posts: 111
Joined: Wed Jun 12, 2013 8:29 am

Re: QT QHostInfo

Wed Jan 31, 2018 11:50 am

EMJB wrote:
Wed Jan 31, 2018 11:28 am
.... it now compiles and runs, but am now looking at why I get a blank name.
Due to my silly mistake - nothing to do with the above problems.

To help anyone else wanting to extract the host name,

(1) My include list in mainwindow.cpp now has "#include "/usr/include/arm-linux-gnueabihf/qt5/QtNetwork/QHostInfo""

(2) My .pro file has "unix:!macx:!symbian: LIBS += {other project specific entries} -lQt5Network"

(3) I found various references on the internet using code that fail to compile for me:
HostName = QHostInfo.localHostName();
HostName = QHostInfo:localHostName();
It should be
HostName = QHostInfo::localHostName();

Thanks again for the help,

EMJB

User avatar
topguy
Posts: 4427
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: QT QHostInfo

Wed Jan 31, 2018 12:07 pm

If you use qmake you should probably just add

Code: Select all

QT           += network
in your .pro file and qmake will handle both include paths and linking automatically.

EMJB
Posts: 111
Joined: Wed Jun 12, 2013 8:29 am

Re: QT QHostInfo

Wed Jan 31, 2018 1:29 pm

topguy wrote:
Wed Jan 31, 2018 12:07 pm
If you use qmake you should probably just add

Code: Select all

QT           += network
in your .pro file and qmake will handle both include paths and linking automatically.
That's a neater solution, thanks. The include statement is now "#include <QHostInfo>", and the LIBS addition is no longer needed.

EMJB

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 6 guests