消息关闭
    暂无新消息!

系统是win7 64,Qt版本5.5
使用QNetworkAccessManager debug调试的时候会出现段错误,release下就没有报错。代码很简单就只是一个demo:不知道是编译器还是什么原因
h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkReply>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void on_pushButton_clicked();
    void readReply(QNetworkReply *reply);
private:
    Ui::MainWindow *ui;
    QNetworkAccessManager *manger;
};

#endif // MAINWINDOW_H

cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkRequest>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    manger = new QNetworkAccessManager();
    connect(manger, &QNetworkAccessManager::finished,
            this, &MainWindow::readReply);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{

    QString temp_url = "http://blog.163.com/";
    QNetworkRequest request;
    request.setUrl(QUrl(temp_url));
    manger->get(request);
}
void MainWindow::readReply(QNetworkReply *reply)
{
    QString html(reply->readAll());
    ui->plainTextEdit->appendHtml(html);
}


1个回答

︿ 1

解决了 估计是作用域的问题 debug模式下估计有的内存区域读取有不同 具体可能还需要大神来解答
这样改就可以了

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QNetworkRequest>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
   //这里注释掉 manger = new QNetworkAccessManager();
   // connect(manger, &QNetworkAccessManager::finished,
            this, &MainWindow::readReply);
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    //在这里添加
    manger = new QNetworkAccessManager();
    connect(manger, &QNetworkAccessManager::finished,
            this, &MainWindow::readReply);
    QString temp_url = "http://blog.163.com/";
    QNetworkRequest request;
    request.setUrl(QUrl(temp_url));
    manger->get(request);


}
void MainWindow::readReply(QNetworkReply *reply)
{
    QString html(reply->readAll());
    ui->plainTextEdit->appendHtml(html);
    //这里做一些事后的补充 删除资源
    reply->deleteLater();
    manager->deleteLater();
    
}

继续更新,并不是作用域的问题。这是个人品问题?代码不动,有时候重启下电脑就好了,有时候就是不行。。。