Help with displaying final value inside a line edit.



  • Hi im struggling with Getting the final value of 'ip' to display inside of the QLineEdit publicIpDisplay2. If i take the section of code from weatherballoon.cpp and place it inside of my main.cpp the qDebug will output my external or public ip to the console. but from the main.cpp i cant get the value of ip which is QHostAddress("XXX.XXX.XX.XXX"). (Id like to remove the QHostAddress section to but unsure of that also), to display inside of the ui form. When the code is placed inside of the weatherballoon.cpp file along with the working local ip display the public ip wont show up only the local. If i comment out the local ip it still doesnt work.

    Heres the code.

    [4_1517193630550_WeatherBalloon.pro](Uploading 100%)

    //your code```
    #-------------------------------------------------
    #
    # Project created by QtCreator 2018-01-28T09:12:26
    #
    #-------------------------------------------------
    
    QT       += core gui network widgets
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = WeatherBalloon
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    
    SOURCES += \
            main.cpp \
            weatherballoon.cpp
    
    HEADERS += \
            weatherballoon.h
    
    FORMS += \
            weatherballoon.ui
    
    ``` here
    

    [3_1517193630550_weatherballoon.ui](Uploading 100%)

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>WeatherBalloon</class>
     <widget class="QWidget" name="WeatherBalloon">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>505</width>
        <height>328</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>WeatherBalloon</string>
      </property>
      <widget class="QLabel" name="ipAddressLabel">
       <property name="geometry">
        <rect>
         <x>40</x>
         <y>20</y>
         <width>91</width>
         <height>17</height>
        </rect>
       </property>
       <property name="text">
        <string>IP ADDRESS:</string>
       </property>
      </widget>
      <widget class="QLabel" name="portLabel">
       <property name="geometry">
        <rect>
         <x>40</x>
         <y>50</y>
         <width>67</width>
         <height>17</height>
        </rect>
       </property>
       <property name="text">
        <string>PORT:</string>
       </property>
      </widget>
      <widget class="QPushButton" name="connectBtn">
       <property name="geometry">
        <rect>
         <x>40</x>
         <y>120</y>
         <width>87</width>
         <height>23</height>
        </rect>
       </property>
       <property name="text">
        <string>Connect</string>
       </property>
      </widget>
      <widget class="QPushButton" name="exitBtn">
       <property name="geometry">
        <rect>
         <x>150</x>
         <y>120</y>
         <width>87</width>
         <height>23</height>
        </rect>
       </property>
       <property name="text">
        <string>Exit</string>
       </property>
      </widget>
      <widget class="QLineEdit" name="ipLineEdit">
       <property name="geometry">
        <rect>
         <x>140</x>
         <y>20</y>
         <width>113</width>
         <height>25</height>
        </rect>
       </property>
      </widget>
      <widget class="QLineEdit" name="portLineEdit">
       <property name="geometry">
        <rect>
         <x>140</x>
         <y>50</y>
         <width>113</width>
         <height>25</height>
        </rect>
       </property>
      </widget>
      <widget class="QLabel" name="publicIpLabel">
       <property name="geometry">
        <rect>
         <x>40</x>
         <y>80</y>
         <width>81</width>
         <height>21</height>
        </rect>
       </property>
       <property name="text">
        <string>PUBLIC IP:</string>
       </property>
      </widget>
      <widget class="QLabel" name="publicIpDisplay">
       <property name="geometry">
        <rect>
         <x>150</x>
         <y>80</y>
         <width>101</width>
         <height>17</height>
        </rect>
       </property>
       <property name="text">
        <string/>
       </property>
      </widget>
      <widget class="QLineEdit" name="publicIpDisplay2">
       <property name="geometry">
        <rect>
         <x>140</x>
         <y>160</y>
         <width>113</width>
         <height>25</height>
        </rect>
       </property>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    
    

    [2_1517193630550_weatherballoon.h](Uploading 100%)

    #ifndef WEATHERBALLOON_H
    #define WEATHERBALLOON_H
    
    #include <QWidget>
    #include <QUdpSocket>
    #include <QTimer>
    #include <QtNetwork>
    
    namespace Ui {
    class WeatherBalloon;
    }
    
    class WeatherBalloon : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit WeatherBalloon(QWidget *parent = 0);
        ~WeatherBalloon();
        double temperature() const;
        double humidity() const;
        double altitude() const;
    
    private slots:
        void sendDatagram();
    
        void on_exitBtn_clicked();
    
    private:
        Ui::WeatherBalloon *ui;
        QUdpSocket udpSocket;
        QTimer timer;
    };
    
    #endif // WEATHERBALLOON_H
    
    

    [1_1517193630550_weatherballoon.cpp](Uploading 100%)

    #include "weatherballoon.h"
    #include "ui_weatherballoon.h"
    
    #include <QtCore>
    #include <QtNetwork>
    #include <cstdlib>
    #include <QMessageBox>
    
    WeatherBalloon::WeatherBalloon(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::WeatherBalloon)
    {
        ui->setupUi(this);
    
        connect(&timer, SIGNAL(timeout()), this,SLOT(sendDatagram()));
    
        timer.start(2 * 0001);
    
        setWindowTitle(tr("Weather Balloon"));
        //public ip address.
        QNetworkAccessManager networkManager;
        QUrl url("https://api.ipify.org");
        //the query used to add the parameter "format=json" to the request
        QUrlQuery query;
        query.addQueryItem("format", "json");
        //set the query on the url
        url.setQuery(query);
    
        //make a *get* request using the above url
        QNetworkReply* reply = networkManager.get(QNetworkRequest(url));
    
        QObject::connect(reply, &QNetworkReply::finished,
                         [&](){
            if(reply->error() != QNetworkReply::NoError) {
                //failure
                qDebug() << "error: " << reply->error();
            } else { //success
                //parse the json reply to extract the IP address
                QJsonObject jsonObject= QJsonDocument::fromJson(reply->readAll()).object();
                QHostAddress ip(jsonObject["ip"].toString());
                //do whatever you want with the ip
                qDebug() << "external ip: " << ip;
                ui->ipLineEdit->setText(ip.toString());
            }
            //delete reply later to prevent memory leak
            reply->deleteLater();
    
        });
        //public ip address end
    //    //Displays the ip address
    //    foreach (const QHostAddress &address, QNetworkInterface::allAddresses())
    //    {
    
    //        if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
    
    //            ui->ipLineEdit->setText(address.toString());
    
    //    }
    
    
    }
    
    WeatherBalloon::~WeatherBalloon()
    {
        delete ui;
    }
    
    double WeatherBalloon::temperature() const
    {
        return -20.0 + (2.0 * std::rand() / (RAND_MAX + 1.0));
    }
    
    double WeatherBalloon::humidity() const
    {
        return 20.0 + (2.0 * std::rand() / (RAND_MAX + 1.0));
    }
    
    double WeatherBalloon::altitude() const
    {
        return 7000 + (100.0 * std::rand() / (RAND_MAX + 1.0));
    }
    
    void WeatherBalloon::sendDatagram()
    {
        QByteArray datagram;
        QDataStream out(&datagram, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_3);
        out << QDateTime::currentDateTime() << temperature() << humidity() << altitude();
    
        udpSocket.writeDatagram(datagram, QHostAddress::LocalHost, 5454);
    
    }
    
    void WeatherBalloon::on_exitBtn_clicked()
    {
        connect(ui->exitBtn, SIGNAL(clicked()), this, SLOT(close()));
    }
    
    

    [0_1517193630549_main.cpp](Uploading 100%) [0_1517193638569_main.cpp]
    (Uploading 100%)

    #include "weatherballoon.h"
    #include "ui_weatherballoon.h"
    #include <QApplication>
    #include <QtNetwork>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        WeatherBalloon w;
        //place code below
        
        
        
        //place code above
        w.show();
    
        return a.exec();
    }
    
    

    To get the code to work and display your ip just copy all from //public ip address --> //public ip address end. place this inside of main.cpp after WeatherBalloon w;. run the code and should get 3 outputs to qDebug.

    qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
    qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
    external ip: QHostAddress("XXX.XXX.XX.XXX")

    if you run the program as is with the public ip code inside the weatherballoon.cpp then only the first two errors get displayed.


  • Moderators

    @MrCrackPotBuilder Did you try without SSL (http instead of https)? It looks like you're missing OpenSSL library. How did you install Qt?



  • Hi, I installed direct from the qt website same as i have done previously.

    I tried without the secure connection and still the same 2 errors and the 2 errors plus the result if main.cpp is used.



  • Ah the error for the sslv2 is because its no longer pre compiled in ubuntu due to it being in secure hhhhmmm thats kind of a big issue for qt to not update. Now i have to reconfigure openssl without the sslv2 no-flag.....



  • You can just put libeay32.dll and ssleay32.dll Librarys in your Build Directory. Works for me. Thats OpenSSL Librarys.


  • Lifetime Qt Champion

    Hi,

    These two are just warnings. They mean that your application is loading a version of OpenSSL that is different from the one used to build Qt. By default Qt doesn't link to OpenSSL because of international laws regarding exporting and import cryptographically enabled software. The library is dlopened.

    By the way, which version of Qt are you using ?
    Which version of OpenSSL did you install ?



  • @Fuel I dont think it would work for me as im using ubuntu not windows.

    @SGaist I'm running ubuntu 16.04 with the latest QT

    Qt Creator 4.4.1
    Based on Qt 5.9.2 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)
    Built on Oct 4 2017 04:14:10
    From revision 6afdb8bdf9



  • Are'nt there Development Packages for OpenSSL on Ubuntu? Like openssl-dev.


  • Moderators

    @MrCrackPotBuilder That's not the Qt version you're using but the one QtCreator was built against. Which Qt version did you install? What is OpenSSL version?



  • @jsulm I have no idea what you mean. Thats all the information from the about section and the install is direct from the qt website i just chose the latest version.

    openSSL 1.0.2g 1 MAR 2016.

    The problem from what i can see with those two errors is just sslv2 is insecure and as such ubuntu prevents it. but QT still needs it. as for using other ssl etc etc i have no idea it took me a few weeks just to learn to get the ip to show. Now all id like is to get the end result displayed into its line edit



  • Why would it work inside the main.cpp and not the weatherballoon.cpp???


  • Qt Champions 2017

    @MrCrackPotBuilder
    It works the same but often when people transfer from main.cpp into a function in other class
    they reuse the code from main and the exec() in main prevents objects from running out of scope
    where as same code in constructor will not.

    WeatherBalloon::WeatherBalloon(QWidget *parent) :
    {
        QNetworkAccessManager networkManager; // this is a local variable
    } // and here it no longer exits.
    

  • Moderators

    @MrCrackPotBuilder said in Help with displaying final value inside a line edit.:

    I have no idea what you mean.

    The information you posted only tells something about QtCreator. QtCreator is NOT Qt, it is an IDE. QtCreator can handle many different Qt versions. So, either you know which Qt version you selected during installation or you go to the Kit you're using in QtCreator and check which Qt version is configured there.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.