To display Ip address in Qlineedit
-
Hi,
What is it that you need ? The way to get the IP address of the system or how to show it in a QLineEdit ?
-
So you are looking for "QNetworkInterface":http://qt-project.org/doc/qt-4.8/qnetworkinterface.html and QLineEdit's "setText":http://qt-project.org/doc/qt-4.8/qlineedit.html#text-prop but you could use a QLabel instead if the QLineEdit will only be used read-only.
-
You have everything you need in the Qt's documentation/demos/example for QLineEdit.
As for QNetworkInterfaces, as a first try:
@QHostAddress firstHostAddress = QNetworkInterfaces::allHostAddresses();
QString hostAddressString = firstHostAddress.toString()
lineEdit->setText(hostAddressString;@ -
thanks @sGaist for your response.. i found another way...
foreach (const QHostAddress &address, QNetworkInterface::allAddresses())
{if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost)) ui->lineedit->setText(address.toString()); }
-
guru are you sure that your code does what you want ?
You're iterating all addresses and only show the last one which is not localhost.
Also, please enclose your code in coding tags, that will make it readable
-
sorry for my late reply Gaist, that code perfectly working and i use it in my linux system...
@
foreach (const QHostAddress &address, QNetworkInterface::allAddresses())
{
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))ui->lineedit->setText(address.toString());
}
@ -
I am not saying it's not working, just that it does not necessarily does what you think.
Keep in mind that if you have several network interfaces you'll only get the last address of the last interface that is not localhost.
-
I also confirm it does, I am just saying that you are lucky because you happen to only have one network interface so both addresses matches. But if you have more like wifi + lan you'll get the address of one of the two interfaces maybe not the one you want, just beware of that, that's all.
-
it displays all interface address. why you are getting the last address is that every time the textbox value is been over written for each entry of the loop. You are thinking that it is only address. try to put qdebug() << address.toString(); you will see the output.
-
it displays all interface address. why you are getting the last address is that every time the textbox value is been over written for each entry of the loop. You are thinking that it is only address. try to put qdebug() << address.toString(); you will see the output.