我正在制作一个Qt项目。为了使它更简单,我决定创建一个类
JsonPostSender
,它是QObject
的子类。我有我的登录对话框,子类化QDialog
。我无法将登录对话框传递给 JsonPostSender
,并且收到以下错误:E0289 no instance of constructor "JsonPostSender::JsonPostSender" matches the argument list
。我的代码有什么问题吗?我该如何修复该错误?
我的代码:
JsonPostSender.h
#include <QObject>
#include <qnetworkaccessmanager.h>
#include <qjsonobject.h>
class JsonPostSender : public QObject
{
Q_OBJECT
public:
JsonPostSender(QObject* parent, QUrl url, QJsonObject json, void (*finishFunc)(QJsonObject));
void sendPost();
void onFinish(QNetworkReply* reply);
~JsonPostSender();
private:
QNetworkAccessManager mgr;
QUrl url;
QJsonObject json;
void (*finishFunc)(QJsonObject);
};
JsonPostSender.cpp
#include "JsonPostSender.h"
#include <qnetworkaccessmanager.h>
#include <qnetworkreply.h>
#include <qnetworkrequest.h>
#include <qjsondocument.h>
JsonPostSender::JsonPostSender(QObject *parent, QUrl url, QJsonObject json, void (*finishFunc)(QJsonObject))
: QObject(parent), url(url), json(json), finishFunc(finishFunc)
{
connect(&mgr, &QNetworkAccessManager::finished, this, &JsonPostSender::onFinish);
}
void JsonPostSender::sendPost() {
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QJsonDocument doc(json);
QByteArray data = doc.toJson();
QNetworkReply* reply = mgr.post(request, data);
}
void JsonPostSender::onFinish(QNetworkReply *reply) {
qDebug() << "Finished";
QJsonObject json = QJsonDocument::fromJson(reply->readAll()).object();
finishFunc(json);
}
JsonPostSender::~JsonPostSender()
{}
登录表单.h
#pragma once
#include <QDialog>
#include "ui_loginForm.h"
#include "qmessagebox.h"
#include "qprogressdialog.h"
#include "QtNetwork/qnetworkaccessmanager.h"
#include "QtNetwork/qnetworkreply.h"
#include "qjsonobject.h"
#include "qjsondocument.h"
class loginForm : public QDialog
{
Q_OBJECT
public:
loginForm(QWidget *parent = nullptr);
~loginForm();
QString auth_token;
public slots:
void setPasswordShow();
void tryLogin();
void onPostFinish(QJsonObject json);
private:
Ui::loginFormClass ui;
};
loginForm.cpp(第29行错误)
#include "loginForm.h"
#include "qmessagebox.h"
#include "qprogressdialog.h"
#include "QtNetwork/qnetworkaccessmanager.h"
#include "QtNetwork/qnetworkreply.h"
#include "qjsonobject.h"
#include "qjsondocument.h"
#include "JsonPostSender.h"
loginForm::loginForm(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.showPassword, &QCheckBox::stateChanged, this, &loginForm::setPasswordShow);
connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &loginForm::tryLogin);
connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &loginForm::reject);
}
void loginForm::tryLogin() {
QJsonObject json;
json["login"] = ui.loginInp->text();
json["passord"] = ui.passwordInp->text();
JsonPostSender sender(this /*<-- here*/, "http://localhost:8301/api/get_token",
json, &loginForm::onPostFinish);
}
void loginForm::onPostFinish(QJsonObject json) {
if (json["token"].isNull()) {
QMessageBox::critical(this, "error", json["error"].toString());
}
else {
auth_token = json["token"].toString();
QMessageBox::information(this, "success", "you're logged in");
accept();
}
}
void loginForm::setPasswordShow() {
if (ui.showPassword->isChecked()) {
ui.passwordInp->setEchoMode(QLineEdit::EchoMode::Normal);
}
else {
ui.passwordInp->setEchoMode(QLineEdit::EchoMode::Password);
}
}
loginForm::~loginForm()
{}
啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊