Google Translator with Qt5



  • Hi... I need to make an application who let me call google translator with the string to translate... I have one example using qt4 (http://developer.nokia.com/Community/Wiki/Qt_Google_Translator), and all the things that I have found are coded with qt4...

    in qt5, all QHttp classes were removed and for that reason, this example dosen't work for me....

    what Am I suppose to do to solve my problem?

    somebody help me, please

    best regards

    Freddy


  • Moderators

    Hi,

    Use "QNetworkAccessManager":http://qt-project.org/doc/qt-5.1/qtnetwork/qnetworkaccessmanager.html for HTTP requests

    Create a QNetworkRequest, and then pass it to QNetworkAccessManager::get()



  • I did it but it dosen't work... this is my code

    @
    #include <QApplication>
    #include <QNetworkAccessManager>
    #include <QUrl>
    #include <QNetworkReply>
    #include <iostream>

    int main(int argc, char argv[])
    {
    QApplication a(argc, argv);
    QNetworkAccessManager
    qnam = new QNetworkAccessManager();
    QNetworkReply* rep = qnam->get(QNetworkRequest(QUrl("https://translate.google.com/?hl=es-419&tab=TT#en/es/correr")));
    QString result = QVariant(rep->readAll()).toString();
    std::cout << result.toStdString();
    qnam->deleteLater();
    return a.exec();
    }
    @


  • Moderators

    Hi,

    Qt is an event-driven framework. get() returns immediately without waiting for the download to finished, so that your program can do other things while downloading (think of large downloads). You need to wait for QNetworkAccessManager or QNetworkReply to emit the finished() signal before you call readAll(). Nokia's QHttp example was the same.

    Connect the finished() signal to a slot that reads the QNetworkReply.

    Some other things:

    To keep things simple, use HTTP instead of HTTPS for now. If you want to use HTTPS, you need to install OpenSSL.

    You don't have to convert the result of readAll() from QByteArray to QVariant. You can convert it directly to QString.

    You can use qDebug() instead of std::cout.

    According to the QNetworkAccessManager documentation, you are responsible for deleting the QNetworkReply when you finish extracting data from it.


Log in to reply
 

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