不完整类型“class QNetworkReply”的无效使用

问题描述

我在尝试让 HTTP 请求在 QT 中工作时遇到很多麻烦,我收到错误:

不完整类型'class QNetworkReply' QByteArray bts = rep->readAll()的无效使用;

我想不通,请帮忙,我做错了什么?我对 Qt 的经验为零

这是我的代码:

mainwindow.cpp

void MainWindow::onfinish(QNetworkReply *rep)
{
    QByteArray bts = rep->readAll();
    QString str(bts);
    QMessageBox::information(this,"sal",str,"ok");

}

void MainWindow::getAPI()
{
    {
        QNetworkAccessManager * mgr = new QNetworkAccessManager(this);
        connect(mgr,SIGNAL(finished(QNetworkReply*)),this,SLOT(onfinish(QNetworkReply*)));
        connect(mgr,mgr,SLOT(deleteLater()));

        mgr->get(QNetworkRequest(QUrl("http://www.google.com")));

}

主窗口.h

class MainWindow;
}

class MainWindow : public QMainWindow
{
 Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void UpdateTime();
    void getAPI();
    void onfinish(QNetworkReply *rep);


private:
    Ui::MainWindow *ui;
    QTimer *timer_1s;

};

.pro 文件

QT       += core gui
QT       += core gui widgets network printsupport

greaterThan(QT_MAJOR_VERSION,4): QT += widgets

TARGET = ApiRequest
TEMPLATE = app


DEFINES += QT_DEPRECATED_WARNINGS

#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui
                 

解决方法

这只是意味着您没有包含标题:

#include <QNetworkReply>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...