开发者

Qt: Trying to do a httprequest, but does not seem to work. What is wrong?

开发者 https://www.devze.com 2023-02-14 14:01 出处:网络
I\'ve been using this example to create a http request for my Qt Nokia application, but I can\'t seem to get it working.

I've been using this example to create a http request for my Qt Nokia application, but I can't seem to get it working.

At first I Tried QHttp, but it is deprecated.

Here's the structure:

mainwindow.cpp
mainwindow.h

request.cpp
request.h

Here's the code:

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    Request* request = new Request();
}

MainWindow::~MainWindow()
{
    delete ui;
}

Request.h

#ifndef REQUEST_H
#define REQUEST_H

#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>

class Request : QObject
{
    //Q_OBJECT
    public:
        Request();
    public slots:
        void finishedSlot(QNetworkReply* reply);
        void checkConnection(QNetworkReply* reply);

};

#endif // REQUEST_H

And btw... what use is the "Q_OBJECT" for?

Request.cpp

#include "request.h"
#include <QDebug>
#include <QMessageBox>

Request::Request()
{
  QNetworkAccessManager* oNetworkAccessManager = new QNetworkAccessManager(this);
  QObject::connect(oNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),this,SLOT(finishedSlot(QNetworkReply*)));
  QObject::connect(oNetworkAccessManager, SIGNAL(networkSessionConnected()),this,SLOT(checkConnection(QNetworkReply*)));

  QUrl url("http://www.redrock.no");
  QNetworkReply* reply = oNetworkAccessManager->get(QN开发者_开发知识库etworkRequest(url));

}

void Request::checkConnection(QNetworkReply* reply)
{
    QMessageBox msgBox;
    msgBox.setText("checkConnection");
    msgBox.setInformativeText("The network session has started");
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();
}

void Request::finishedSlot(QNetworkReply* reply)
{

    QMessageBox msgBox;
    msgBox.setText("checkConnection");
    msgBox.setInformativeText("The request is done");
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();
}

The message boxes is just since I don't have a usb cable for my phone.

I have set breakpoints at both the "checkConnection" slot and the "finishedSlot" slot, but nothing happens there.

Anyone got an idea of what I could be doing wrong?


Here is an explanation of the Q_OBJECT macro:

  • http://doc.qt.nokia.com/latest/qobject.html#Q_OBJECT

Among other things,

The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots...

So I would first try uncommenting that and see what changes. It should at least get your signals and slots talking to one another. (This is a Qt-specific feature, not part of C++, and needs the Q_OBJECT macro.) I didn't look carefully at the rest of your code, because that is the obvious first thing to try. Actually, I am surprised that it would even compile without that.


ok, finally i found what's wrong... and as usual, it's just a minor error from me.

I uncommented the Q_OBJECT, and got some vtable error or something like that. I did get this error message earlier today when i had the Q_OBJECT there, and that is why i commenting it.

But since i'm new to Qt i hade forgot to incelude the QObject in the request.h "#include "

And that fixed everything for me :)

Thanks for the explenation and elaboration Dave.

0

精彩评论

暂无评论...
验证码 换一张
取 消