setting ip address using qt
Page |
1 |
Hi,
I am working on a project(Embedded device), in which I have to change the write an application for changing the IP address using Qt.
Typically the application should ask for the option for changing the IP address and if user enters a valid IP address then that IP address should be set. I know I have to make use of QHostaddress in Qt but I want some more info and a link for example code will be very helpful.
I appreciate any suggestion regarding this.
Thanks
16 replies
QHostAddress just allows you to hold an IPv4 or IPv6 address in a platform- and protocol-independent manner. It doesn’t allow for changing the address of a network interface.
As far as I know you will need platform-specific code, for example invoking ifconfig or netsh using QProcess or altering the interface using DBus.
If you like to input the ip address and validate it only, this might help [developer.qt.nokia.com]
Hi,
I am working on an embedded project (Linux os running on ARM 9 processor). Qt is used for GUI.
currently I have to develop an application which can set/reset the Ip address of the device using Qt application. Till now I am able to get the IP address of the device through Qt by using Qprocess and ifconfig etc but I am not been able to change the Ip address.
Here is the snippet :
- {
- QString setbuff;
- char* ip_buff = "192.168.2.238";
- setbuff.sprintf("ifconfig eth0 %s netmask 255.255.255.0 up",ip_buff);
- qDebug() << setbuff;
- qProc->start(setbuff);
- qDebug() << setbuff;
- qProc->waitForFinished();
- }
The set buff content is showing correctly as : “ifconfig eth0 192.168.2.238 netmask 255.255.255.0 up”
But when the code is cross compiled and run on the target embedded device, the Ip address is not setting as 192.168.2.238.
BTW the target device is executing the code in root mode.
Please help me solve this problem
Edit: please format code sections using @ tags
Hi volker..
For changing the ip adress, first I developed a small c application using linux system call and also successfully changed it. eg I used “ifconfig eth0 192.168.2.200 netmask 255.255.255.0 up”. Al of this stuff I did it in root mode on PC.
I did a few modifications for Qt as mentioned above and tried to run but not been able to change the IP address.I agree to what you said that I should have root access to change the IP address but the code is running the code on the embedded device in the root mode. Do I have to provide any root permission through Qt?
Please suggest any idea if you have.
Thanks
I truly appreciate your reply volker..
Do you mean I can write an application in c and include it in Qt directly? If you have any idea about it please elaborate it. Any links of how to do it and run it will also be very helpful.I again want to mention that this code will be used on an embedded device.
Please help me regarding this.
Waiting for your reply.
Thanks
You never write an application in language X and “include” it into another application.
You have your C application to set the IP address. That contains some code (snippet), hopefully in a function or method. You can integrate that snippet in any other C or C++ application and call that method. This has nothing to do with embedded or desktop.
I developed a c application for changing the Ip address and it worked perfectly fine.I made the changes as mentioned in the above code snippet.I also made the changes and used arg() instead of sprintf() as per your suggestion but still I am not been able to change the Ip address. Can you please go through the above snippet of code and let me know if I have done any mistakes?
BTW the target device is executing the code in root mode.
The code executing and running on the linux embedded device is in root mode but still the code is not working fine.
Al of this stuff I did it in root mode on PC.
I agree to what you said that I should have root access to change the IP address but the code is running the code on the embedded device in the root mode.
Is your application now executed as root or isn’t it?
If not: you cannot elevate privileges from an unprivileged process. You will have to use an external application having the setuid bit set, like sudo, or you will have to set the setuid bit on your application itself (don’t do this unless you know what you are doing).
You must log in to post a reply. Not a member yet? Register here!




