2 u011094358 u011094358 于 2015.06.03 11:13 提问

QT 简单QNetwork历程无法使用

新手求教,历程无法使用,在debug的时候在replyfinished函数开始处设置断点,一直没有触发
这是pro文件:
QT += core gui network
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = weatherget
TEMPLATE = app

SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui

这是头文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include
#include
#include
#include

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget parent = 0);
~MainWindow();
private:
void replyFinished(QNetworkReply
);
private:
Ui::MainWindow *ui;
QNetworkAccessManager *manager;

};

#endif // MAINWINDOW_H

这是源文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
MainWindow::MainWindow(QWidget parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QUrl url(QString("http://www.weather.com.cn/adat/sk/101010100.html"));
manager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
request.setUrl(url);
manager->get(request);
connect(manager,SIGNAL(finished(QNetworkReply
)),this,SLOT(replyFinished(QNetworkReply*)));
}

MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
QTextCodec *codec = QTextCodec::codecForName("utf8");
QString all=codec->toUnicode((reply->readAll()));
ui->textBrowser->setText(all);
reply->deleteLater();
}

3个回答

u011094358
u011094358   2015.06.03 12:05
已采纳

知道哪里错了,replyfinished函数没有在h文件里声明为publick slots,问题结束

feng1790291543
feng1790291543   Ds   Rxr 2015.06.05 10:51

楼主,1、你写的这里
“private:
void replyFinished(QNetworkReply);”
并不是 slot槽;
2、“connect(manager,SIGNAL(finished(QNetworkReply)),this,SLOT(replyFinished(QNetworkReply*)));” 中 信号和槽 的 参数不一致,一个是 对象,一个是指针;
改过来就好了

datou431
datou431   Rxr 2015.06.18 15:34

replyfinished函数没有在h文件里声明为publick slots,问题结束

Csdn user default icon
上传中...
上传图片
插入图片