Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. To display Ip address in Qlineedit

To display Ip address in Qlineedit

Scheduled Pinned Locked Moved C++ Gurus
16 Posts 4 Posters 9.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    guru
    wrote on last edited by
    #1

    how to display Local system Ip address, by using Qlineedit in a dialog. in this case I use lineedit at readonly mode.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        guru
        wrote on last edited by
        #3

        i wanna get ip address of the system and also to show it in a QLineedit..

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            guru
            wrote on last edited by
            #5

            i need a small example.. be coz im new to qt tool..

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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;@

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • G Offline
                G Offline
                guru
                wrote on last edited by
                #7

                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());
                
                }
                
                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bipll
                  wrote on last edited by
                  #8

                  @ui->lineedit->setText("127.0.0.1");@
                  Can I get my prize now? 8-)

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      guru
                      wrote on last edited by
                      #10

                      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());
                      }
                      @

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          guru
                          wrote on last edited by
                          #12

                          but it displays one address in lineedit box.. i aslo check that address in my winxp os.. its same ip.

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            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.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              guru
                              wrote on last edited by
                              #14

                              yeah, its correct, now im using pc, i connected through LAN, thanks for your suggesstion, i will consider in my future..

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                RohitIti
                                wrote on last edited by
                                #15

                                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.

                                1 Reply Last reply
                                0
                                • R Offline
                                  R Offline
                                  RohitIti
                                  wrote on last edited by
                                  #16

                                  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.

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved