我正在制作一个 gui 应用程序,我将数据发送到我的 Web 服务并接收 JSON 响应。使用调试器,我发现我设法发送数据并从服务器接收响应。但是,应用程序在明文中的值更改之前崩溃。一旦我将AESENkriptiraj中的数组数量更改为两个,我就成功了,但即便如此,向按钮发送垃圾邮件也会使应用程序崩溃。问题可能出在我的线程管理中,但我不知道它是什么。
解密窗口.h
#ifndef DECRYPTIONWINDOW_H
#define DECRYPTIONWINDOW_H
#include <QWidget>
#include "QFile.h"
#include "qfiledialog.h"
#include "qtextstream.h"
#include "kriptiraj.h"
namespace Ui {
class DecryptionWindow;
}
class DecryptionWindow : public QWidget
{
Q_OBJECT
public:
explicit DecryptionWindow(QWidget *parent = 0);
~DecryptionWindow();
signals:
public slots:
void AESkriptiranoVrati(QString Poruka);
private:
private slots:
void on_btnFilePath_clicked();
void on_btnKriptirajAES_clicked();
void on_btnDekriptirajAES_clicked();
private:
Ui::DecryptionWindow *ui;
};
#endif // DECRYPTIONWINDOW_H
解密窗口.cpp
#include "decryptionwindow.h"
#include "ui_decryptionwindow.h"
#include <QTextCodec>
Kriptiraj *kriptiraj= new Kriptiraj();
DecryptionWindow::DecryptionWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::DecryptionWindow)
{
ui->setupUi(this);
connect(kriptiraj, SIGNAL(Poruka(QString)), this, SLOT(AESkriptiranoVrati(QString)));
}
void DecryptionWindow::on_btnKriptirajAES_clicked()
{
kriptiraj->AESEnkriptiraj(ui->txtSadrzajDatotekeAES->toPlainText(), ui->txtKljucAES->toPlainText(), "AES", "Kriptiraj");
}
void DecryptionWindow::AESkriptiranoVrati(QString poruka){
ui->txtSadrzajDatotekeAES->setPlainText(poruka);
}
kriptiraj.h
#ifndef KRIPTIRAJ_H
#define KRIPTIRAJ_H
#include <qobject.h>
#include <qnetworkaccessmanager.h>
#include <qthread.h>
#include <QtScript/qscriptengine>
#include <QtScript/QScriptValueIterator>
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QEventLoop>
#include <QDebug>
class Kriptiraj : public QObject
{
Q_OBJECT
public:
explicit Kriptiraj();
~Kriptiraj();
signals:
Nastavi();
Poruka(QString);
public slots:
void AESEnkriptiraj(QString tekst, QString kljuc, QString metoda, QString zahtjev);
QString IzvuciPodatak(QNetworkReply* reply);
private:
QNetworkAccessManager *networkManager;
private slots:
};
#endif // KRIPTIRAJ_H
kriptiraj.cpp
#include "kriptiraj.h"
Kriptiraj::Kriptiraj()
{
networkManager= new QNetworkAccessManager();
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(IzvuciPodatak(QNetworkReply*)));
}
void Kriptiraj::AESEnkriptiraj(QString tekst, QString kljuc, QString metoda, QString zahtjev){
QNetworkReply* Reply;
QString url ="http://jospudjaatfoi.000webhostapp.com/OS2/AESenkripcija.php?tekst="+tekst+"&kljuc="+kljuc+"&metoda="+metoda+"&zahtjev="+zahtjev;
QUrl Url(url);
QNetworkRequest Request;
Request.setUrl(Url);
Reply = networkManager->get(Request);
}
QString Kriptiraj::IzvuciPodatak(QNetworkReply* reply){
QString data = (QString) reply->readAll();
//reply->deleteLater();
QJsonDocument JSON= QJsonDocument::fromJson(data.toUtf8());
QJsonObject JSONObject = JSON.object();
QJsonValue JSONValue = JSONObject.value("result");
QString link =JSONValue.toString();
emit Poruka(link);
}
Kriptiraj::~Kriptiraj()
{
}
基本上我有两个连接,一个在解密窗口的初始化中,另一个在 kriptiraj 的初始化中。当我运行on_btnKriptirajAES_clicked()时代码继续运行AESENKriptiraj发送一个获取请求,当完成使用连接时运行IzvuciPodatak将Poruka返回到运行AESkriptiranoVrati的解密窗口.cpp 连接。之后它崩溃了。
很抱歉命名和其他做法不好,也很抱歉在我的代码中使用母语和英语。