我是C ++和Qt的初学者,我试图制作一个翻译程序,但它在网络请求执行时崩溃......
错误: Oversetter.exe已退出,代码为255
我的代码:
标头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QApplication>
...
#include <QUrl>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtScript/QScriptEngine>
#include <QtScript/QScriptValue>
#include <QtScript/QScriptValueIterator>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow();
public slots:
void getTranslation();
void onTranslationRecieved(QNetworkReply* reply);
private:
QTextEdit *te_input;
QTextEdit *te_output;
};
#endif
代码文件
void MainWindow::getTranslation()
{
te_output->setText("[Oversettelse pågår]\n[Translation in progress]");
QString url = "http://translate.googleapis.com/translate_a/single?client=gtx";
...
url.append(te_input->toPlainText());
qInfo() << "url: " + url;
QNetworkAccessManager *nam;
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(onTranslationRecieved(QNetworkReply*)));
nam->get(QNetworkRequest(QUrl(url)));
}
void MainWindow::onTranslationRecieved(QNetworkReply* reply)
{
QString data = (QString) reply->readAll();
te_output->setText(data);
QScriptEngine engine;
QScriptValue result = engine.evaluate(data);
QString translation = result.property("0").property("0").property("0").toString();
}
非常感谢任何帮助: - )
答案 0 :(得分:1)
首先:
行
QNetworkAccessManager *nam;
应该是:
QNetworkAccessManager *nam = new QNetworkAccessManager(this);