QT网络回复不会回复PHP脚本的结果

时间:2012-01-30 17:35:56

标签: php c++ xml qt qnetworkaccessmanager

大家好我有一个问题。 我正在连接QT与PHP脚本..一切正常,但可以somone帮助我如何从它接收PHP结果。 这是我的代码:

void MainWindow::Dodaj_korisnika(QUrl url)
{
    //reply = qnam.get(QNetworkRequest(url));
    ////////////////////////////////////////////////////
    QUrl params;
    params.addQueryItem("action","Dodaj_korisnika");
    params.addQueryItem("ime",ui->lineEdit);
    params.addQueryItem("prezime",ui->lineEdit_2);
    params.addQueryItem("broj",ui->lineEdit_3);
    params.addQueryItem("adresa",ui->lineEdit_4);

        QByteArray data;
        data.append(params.toString());
        data.remove(0,1);

        QNetworkRequest request;
        request.setUrl(url);
        request.setHeader(QNetworkRequest::ContentTypeHeader,
        QVariant("application/x-www-form-urlencoded"));
        QNetworkAccessManager *manager = new QNetworkAccessManager(this);
        QNetworkReply *reply = manager->post(request, data);
        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}

我在论坛上发现我需要使用此功能:

void MainWindow::replyFinished(QNetworkReply *reply)
{
    QString data = reply->readAll().trimmed();
    // document.setContent(reply);
}

但是,我没有得到任何东西进入这个功能..是否有可能从QT经理变量读取?我收到xml结果。我想把它作为QDomDocument可能吗? 如果你有时间,请给我一些提示或一些例子.. 谢谢所有!

1 个答案:

答案 0 :(得分:1)

你确定PHP脚本收到了什么吗?

  • 首先,每次发布时都会创建一个信号和插槽
  • 其次,您在发布
  • 后创建连接
  • 第三次验证您的查询格式正确并且php脚本正在接收它。如果您有脚本log \ print传入消息

编辑:这是它应该是什么样子

// early in the code, like in the constructor 
void MainWindow::init(){
  this->manager = new QNetworkAccessManager(this);
  connect(this->manager, SIGNAL(finished(QNetworkReply*)), 
          this, SLOT(replyFinished(QNetworkReply*)));
}

void MainWindow::Dodaj_korisnika(QUrl url){

    QUrl params;
    params.addQueryItem("action","Dodaj_korisnika");
    params.addQueryItem("ime",ui->lineEdit);
    params.addQueryItem("prezime",ui->lineEdit_2);
    params.addQueryItem("broj",ui->lineEdit_3);
    params.addQueryItem("adresa",ui->lineEdit_4);

    QByteArray data;
    data.append(params.toString());
    data.remove(0,1);

    QNetworkRequest request;
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader,
    QVariant("application/x-www-form-urlencoded"));

    //Here you don't need to get the reply, it will be the same given to 
    //MainWindow::replyFinished
    this->manager->post(request, data);

}

void MainWindow::replyFinished(QNetworkReply *reply){
    //Use the reply as you wish
}