写qt c++时遇到的问题
目的是写一个从目标地址下载图片的软件
代码在connect那一行出问题了
按钮clicked事件本来写的是&QPushButton::clicked,但是看到example中的http项目里面用的是&QAbstractButton::clicked,于是随示例更正了,
但是问题还是没有解决
代码如下:
//imageDownloader.h
#ifndef IMAGEDOWNLOADER_H
#define IMAGEDOWNLOADER_H
#include <QWidget>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include <QLineEdit>
#include <QPushButton>
QT_BEGIN_NAMESPACE
namespace Ui { class imageDownloader; }
QT_END_NAMESPACE
class imageDownloader : public QWidget
{
Q_OBJECT
public:
imageDownloader(QWidget *parent = nullptr);
~imageDownloader();
private slots:
void DownloadImageAynsc(const QUrl &ImageUrl);
void StartDownload();
private:
Ui::imageDownloader *ui;
QLineEdit *urlLineEdit;
QPushButton *StartBt;
};
#endif // IMAGEDOWNLOADER_H
//imageDownloader.cpp
#include "imageDownloader.h"
#include "ui_imagedownloader.h"
imageDownloader::imageDownloader(QWidget *parent)
: QWidget(parent)
, ui(new Ui::imageDownloader)
, urlLineEdit(ui->urlLineEdit)
, StartBt(ui->StartButton)
{
ui->setupUi(this);
connect(StartBt,&QAbstractButton::clicked,this,&imageDownloader::StartDownload);
}
imageDownloader::~imageDownloader()
{
delete ui;
}
void imageDownloader::StartDownload(){
QUrl *imageUrl = new QUrl(this->urlLineEdit->text());
DownloadImageAynsc(*imageUrl);
}
void imageDownloader::DownloadImageAynsc(const QUrl &ImageUrl){
// 创建QNetworkAccessManager实例
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
// 创建下载请求
QNetworkRequest request(ImageUrl);
// 发送下载请求
QNetworkReply *reply = manager->get(request);
// 监听下载进度和完成事件
connect(reply, &QNetworkReply::downloadProgress ,[=](qint64 bytesReceived, qint64 bytesTotal) {
// 更新下载进度
// 可以在此处更新进度条或其他UI元素
qDebug() << bytesReceived << "/" << bytesTotal << endl;
});
connect(reply, &QNetworkReply::finished ,[=]() {
// 下载完成,检查是否有错误
if (reply->error() == QNetworkReply::NoError) {
// 保存下载的图片到本地文件
QFile file("image.jpg");
if (file.open(QIODevice::WriteOnly)) {
file.write(reply->readAll());
file.close();
}
} else {
// 处理下载错误
qDebug() << "Download failed: " << reply->errorString();
}
// 删除QNetworkReply实例
reply->deleteLater();
});
}
问题似乎是出在这了,有无hxd帮我看看