如何允许 QNetworkAccessManager 在单独的线程中运行 HTTP 请求

发布于 2024-12-19 03:09:33 字数 186 浏览 3 评论 0原文

我希望 QNetworkAccessManager 在单独的线程中运行 HTTP 请求。目前在 QT4.6 中,它在同一线程中运行,导致我的浏览器挂起。 这个功能最近在 QT 4.8 中引入,但现在我无法切换到 QT 4.8。因此我想在 QT 4.6 中为 QNetworkAccessManager 实现这一点。

有人能帮我解决这个问题吗?

I want QNetworkAccessManager to run HTTP requests in seperate thread. Currently in QT4.6 it is running in the same thread and it causes my browser to hang.
This feature is recently introduced in QT 4.8 but now I cant switch to QT 4.8. therefore I want to implement this in QT 4.6 for QNetworkAccessManager .

Can anyone help me on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

篱下浅笙歌 2024-12-26 03:09:33

有多种方法可以实现您的愿望。

首先,确保您正确使用 QNetworkAccessManager。默认情况下,HTTP 请求(例如:)

QNetworkAccessManager *manager= new QNetworkAccessManager(this);
manager->post(QNetworkRequest(QUrl("http://www.example.com/")));

是异步发出的,但这并不一定意味着它们位于自己的线程中。如果进行大量此类调用,可能会减慢包含线程的速度。

现在,我用来确保在单独的线程中发出请求的一种方法是为我的 QNetworkAccessManager 创建一个完整的 QObject/QWidget,如下所示:

(标题)

class Manager : public QWidget
{
    Q_OBJECT

public:
    Manager(QWidget *parent=0);
    QNetworkAccessManager *manager;
private slots:
    void replyFinished(QNetworkReply* data);
};

//... ... ...
//Later in the main thread declaration
//... ... ...

class MainBrowserWindow : public QWidget
{
     //.... ... .. .. 
     //Other stuff for the main window

     Manager managingWidget;
     //this ensures that a new thread will be created and initialized
     //alongside our MainBrowserWindow object (which is initialized in main.cpp)

};

(实现)

Manager::Manager(QWidget *parent): QWidget (parent){
    //Initialize the widget here, set the geometry title and add other widgets
    //I usually make this a QWidget so that it can double as a
    //pop-up progress bar.

    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
}

现在,您可以从主窗口的实现中调用管理器对象调用如下:

managingWidget.manager->post()

再次强调,这只是您可以使用的众多方法之一,在某些情况下,QNetworkAccessManager 会自动将请求放置在它们自己的线程中。但这会强制操作系统将所有请求放置在与主线程分开的线程中。

There are more than a couple ways to accomplish what you desire.

First, make sure you are using the QNetworkAccessManager correctly. By default HTTP requests, such as:

QNetworkAccessManager *manager= new QNetworkAccessManager(this);
manager->post(QNetworkRequest(QUrl("http://www.example.com/")));

are made asynchronously, but this does not necessarily mean they are in their own thread. If you make a bunch of these calls, you could slow down the containing thread.

Now, one way that I use to ensure requests are made in separate threads is to create an entire QObject/QWidget for my QNetworkAccessManager like this:

(Header)

class Manager : public QWidget
{
    Q_OBJECT

public:
    Manager(QWidget *parent=0);
    QNetworkAccessManager *manager;
private slots:
    void replyFinished(QNetworkReply* data);
};

//... ... ...
//Later in the main thread declaration
//... ... ...

class MainBrowserWindow : public QWidget
{
     //.... ... .. .. 
     //Other stuff for the main window

     Manager managingWidget;
     //this ensures that a new thread will be created and initialized
     //alongside our MainBrowserWindow object (which is initialized in main.cpp)

};

(Implementation)

Manager::Manager(QWidget *parent): QWidget (parent){
    //Initialize the widget here, set the geometry title and add other widgets
    //I usually make this a QWidget so that it can double as a
    //pop-up progress bar.

    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
}

Now you can make calls to your manager object from your main window's implementation with calls like:

managingWidget.manager->post()

Once again, this is only one of the many methods you may use, and in some instances the QNetworkAccessManager will automatically place requests in their own thread. But this should force the operating system to place all of your requests in a thread separate from your main thread.

本宫微胖 2024-12-26 03:09:33

阅读Qt 中的线程,并创建自己的线程来接收每个 URL 的信号处理,并在处理 HTTP 回复时向主线程发出一些信号。

Read about threads in Qt, and make your own thread which recieve signal for each URL to process, and emit some signal back to the main thread when the HTTP reply is handled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文