logo
down
shadow

Qt execute multiple get with QNetworkAccessManager


Qt execute multiple get with QNetworkAccessManager

Content Index :

Qt execute multiple get with QNetworkAccessManager
Tag : cpp , By : KT.
Date : January 02 2021, 06:48 AM

will help you I have tested your code and I have not noticed that the GUI is blocked, in addition to using signals and slots you should not have that problem. However I see that you are creating dynamic memory unnecessarily, in the following code I connect the QNetworkReply to a slot, filter through the QUrl and delete the QNetworkReply:
*.h
...
private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void onReplyfinished();

private:
    Ui::MainWindow *ui;
    QNetworkAccessManager *manager;
...
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    manager = new QNetworkAccessManager(this);
}

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

void MainWindow::on_pushButton_clicked()
{
   QNetworkReply *reply = manager->get(QNetworkRequest{QUrl{"http://www.link.com/path/"}});
   connect(reply, &QNetworkReply::finished, this, &MainWindow::onReplyfinished);
}

void MainWindow::on_pushButton_2_clicked()
{
   QNetworkReply *reply = manager->get(QNetworkRequest{QUrl{"https://ergast.com/api/f1/2008/5.json"}});
   connect(reply, &QNetworkReply::finished, this, &MainWindow::onReplyfinished);
}

void MainWindow::onReplyfinished()
{
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
    if(reply->url() == QUrl{"http://www.link.com/path/"}){
        ui->lineEdit->setText(reply->error() ? "error": reply->readAll());
    }
    else if (reply->url() == QUrl{"https://ergast.com/api/f1/2008/5.json"}){
        ui->lineEdit_2->setText(reply->error() ? "error": reply->readAll());
    }
    reply->deleteLater();
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Qt QNetworkAccessManager and multiple QNetworkReply


Tag : qt , By : krs
Date : March 29 2020, 07:55 AM
will be helpful for those in need You can create a RequestSender class whose role is looking after requests.
Each RequestSender object will handle one unique request. While creating the QNetworkRequest that will be sent, the RequestSender will "tag" its own request with the originatingObject attribute. This attribute indicates which object sent the request. When a RequestSender object receives a reply, it will look if it is the sender of the request via the originatingObject attribute. For further informations about originatingObject, you can refer to the documentation here : http://qt-project.org/doc/qt-4.8/qnetworkrequest.html#originatingObject
class RequestSender {
    public:
        RequestSender();
        ~RequestSender();
        void createRequest(/* Request parameters */);

    public slots:
        void endRequest(QNetworkReply* replay);
};
RequestSender::RequestSender() {
    connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(endRequest(QNetworkReply*)));
}

RequestSender::~RequestSender() {
    disconnect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(endRequest(QNetworkReply*)));
}

void RequestSender::createRequest(/* Request parameters */) {
    QNetworkRequest * myRequest = 0;

    // Build myRequest with the request parameters

    myRequest->setOriginatingObject(this);
    nam->get(*myRequest);

}

void RequestSender::endRequest(QNetworkReply* replay) {
    if (replay->request().originatingObject() != this) {
        // That's not the request sent by the object -> stop the method here !
        return;
    }

    // Treatments on replay
}

Qt QNetworkAccessManager & multiple QNetworkReplay


Tag : qt , By : Arun Thomas
Date : March 29 2020, 07:55 AM
Any of those help maybe you can do something like this: having an enum of the different methods
enum GetMethod
{
    getUserId,
    getUserDetails
};
QHash<QNetworkReply*, GetMethod> hash;

QNetworkReply *reply1 = nam->post(requestUserId, data);
hash[reply1] = GetMethod::getUserId;

QNetworkReply *reply2 = nam->post(requestUserDetails, data);
hash[reply2] = GetMethod::getUserDetails;

connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
void MainWindow::finished(QNetworkReply *reply)
{
    switch(hash[reply])
    {
    case GetMethod::getUserId:
        GetUserIDCompleted(reply);
        break;
    case GetMethod::getUserDetails:
        GetUserDetailsCompleted(reply);
        break;
    }

    hash.remove(reply);
}

error: no matching function for call to 'QNetworkAccessManager::QNetworkAccessManager(Networking* const)'


Tag : cpp , By : Eniac
Date : March 29 2020, 07:55 AM
I wish did fix the issue. This is my files. I have some problems with that. , Add Q_OBJECT macro to your class without ;
class Networking : public QObject
{
Q_OBJECT
public:
    Networking();
    void getNetReply();

using QNetworkAccessManager GET multiple times


Tag : cpp , By : user161380
Date : March 29 2020, 07:55 AM
this one helps. I am writing an application to request a web page at equal intervals in order to get any changes in it (to check whether new data is received). here how i did it. , I see following problems:

trying to send file using QNetworkAccessManager get() but not QNetworkAccessManager post()


Tag : cpp , By : nhuser
Date : March 29 2020, 07:55 AM
To fix the issue you can do I am a newbie, I am trying to send a .xml file to server using Qt4.8. My restriction is , I am told to use QNetworkAccessManager->get() only. Although QNetworkAccessManager->post is available but I can't use it as per instruction given to me. So, write following code:- , I figure it out, you have to add data as query. code will like this:-
bool PIS::sendPISData(QString xmlFile,QString IP)
{
    QFile xmlfyle(xmlFile);
    if(!xmlfyle.open(QIODevice::ReadOnly))
    {
        #ifdef DEBUG
        qDebug("Can not open file device.");
        #endif
    }

    QString content = (QString)xmlfyle.readAll();
    #ifdef DEBUG
    qDebug()<<"content :::  "<<content;
    #endif
    xmlfyle.close();
    QNetworkAccessManager mgr;
    QEventLoop eventLoop;
    QUrl url(QString("http://"+IP+"/Smart_Coach_Service/data_interchange/senddata"));
    url.addQueryItem("xml_data", content);
    QNetworkReply *replyn = mgr.get(reqpn);
    connect(replyn, SIGNAL(finished()), &eventLoop, SLOT(quit()));
    eventLoop.exec(); 
    return true;
} 
Related Posts Related QUESTIONS :
  • how is push_back implemented in STL vector?
  • C++ Profiling: KiFastSystemCallRet
  • Multiple rows with a single INSERT in SQLServer 2008
  • Use super class's address/pointer in initialization list
  • double fork using vfork
  • Convert char pointer (char*) to struct
  • Does anyone have good tips for using pc-lint?
  • C++ How fast is passing around objects?
  • template type's ctor arguments at instantiation time
  • Get list of windows in taskbar on Windows 7?
  • Curl connection options
  • Best cross-platform solution for network server?
  • simple c++ file opening issue
  • Switching from C++ (with a lot of STL use) to C for interpreter building
  • How can I access the JPEG image pixels as a 3D array like we do in MATLAB?
  • What wording in the C++ standard allows static_cast<non-void-type*>(malloc(N)); to work?
  • Avoid allocating in constructor or preserve simplicity (and RAII?)
  • Can C++ raise an error when std array initialization is too small?
  • Reference to end is ambiguous
  • Problem with basic usage of std::enable_if
  • How to print out a decimal number as octal number using setiosflags in C++
  • Open Visual Studio with solution and specific file and line
  • Enum value based on template type
  • Is there any way to swap nodes in std::list?
  • How to copy QString into wchar_t buffer
  • Make the compiler generate an empty default function for an std::function
  • Insert or push_back to end of a std::vector?
  • Best practice for const temporary types
  • Include CSV File in Complied Binary
  • Issue with binding non static function to callback
  • How can read from some files and write content of them with different form into files with same names
  • Why does auto deduce this variable as double and not float?
  • no instance of overloaded function "std::make_unique" matches the argument list, but works with unique_ptr con
  • How to see array size from a pointer in c++
  • Error taking address of temporary in Eclipse
  • Using an iterator to go through a vector and modify the contents
  • Are extern extern "C", and extern "C" extern, allowed?
  • Can't solve C2660 and C2065 Errors
  • C referencing C++ extern
  • How to write the definition of a derived class in c++?
  • Why when I include <cmath> I need to use the namespace std too?
  • How to assign a 32-bit unsigned integer to a bit field containing 32 bits
  • Why does the same class being defined in multiple .cpp files not cause a linker multiple definition error?
  • C++ 11db error when trying to quit the program in xcode. beginner level
  • Add content of a vector into a Capnproto map object
  • Recursively Pass Template Template To a Template Template Function
  • Swap rows in a 2D array with std::swap. How does it work?
  • Is there any situation in which an object's storage might change during its lifetime?
  • clang++ always generates empty profraw coverage reports
  • Do memory leaks persist after program completion if the OS does not clear it?
  • How to link library using cmake
  • How to use getters and setters without generating a copy?
  • Generating multiple amounts of the same sprite broken
  • function in c++ why my compiler didn't recognize the error()
  • Relationship between copy(...) and copy(seq, ...)
  • Are <cmath> functions required to be `noexcept` in C++17?
  • How to find a struct list item
  • How can you handle DLL versions when referencing C++ DLL's over COM from VBScript with CreateObject?
  • Do not understand how c++ set works
  • Actual build date in C++
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com