简体   繁体   English

获取在replyFinished()中启动QNetworkRequest的函数

[英]Get function which initiated QNetworkRequest in replyFinished()

i have following problem: 我有以下问题:

I have a class Foo which encapsulates a web-api. 我有一个类Foo,它封装了一个web-api。 the interface has following functions: 界面具有以下功能:

Foo::addItem( QString id ) Foo :: addItem(QString id)
Foo::updateItem( QString id ) Foo :: updateItem(QString id)

both function initiate a QNetworkRequest with the same URL but the usage of the data is different. 两个函数都使用相同的 URL启动QNetworkRequest,但数据的使用情况不同。 Therefore i need to know in slot function Foo::replyFinished( QNetworkReply *wf_reply ) from where the QNetworkRequest originated. 因此我需要知道QNetworkRequest发起的插槽函数Foo :: replyFinished(QNetworkReply * wf_reply)

How would you solve this? 你怎么解决这个问题?

I could use variable to store the adress of the QNetworkRequest to compare it later to wf_reply->request() but this seems like a hack to me. 我可以使用变量存储QNetworkRequest的地址,以便稍后将其与wf_reply-> request()进行比较,但这对我来说似乎是一个黑客攻击。 Considering you can call addItem() or updateItem() hundred times before replyFinished() is executed for the first time. 考虑到你可以在第一次执行replyFinished()之前调用addItem()或updateItem()一百次。 The best way would be to add a sting or integer to QNetworkRequest which contains the function name or id. 最好的方法是向QNetworkRequest添加一个包含函数名称或id的sting或整数。

In your original QNetworkRequest you can set an attribute with 在原始的QNetworkRequest您可以使用设置属性

setAttribute(Attribute code, const QVariant & value)

Attribute is an enum and there is a reserved code for just this situation, QNetworkRequest::User . Attribute是一个枚举,只有这种情况有一个保留代码, QNetworkRequest::User (See: Attribute ) (参见: 属性

In your QNetworkReply , you can pull the QNetworkRequest with request() then get the Attribute from there with attribute() QNetworkReply ,您可以使用request()获取QNetworkRequest ,然后使用attribute()从那里获取Attribute

Bit of a hack, but I think it should work. 一点点黑客,但我认为它应该工作。

You have four options: 您有四种选择:

  1. Connect void QNetworkReply::finished() signal of each reply you create with one of your functions handling specific kind of requests 使用处理特定类型请求的某个函数连接您创建的每个回复的void QNetworkReply::finished()信号
  2. Use setAttribute() in QNetworkRequest as suggested by Brian 如Brian所建议的那样在QNetworkRequest中使用setAttribute()
  3. Use void QNetworkRequest::setOriginatingObject(QObject * object) which was added just for this purpose. 使用为此目的添加的void QNetworkRequest::setOriginatingObject(QObject * object) The problem in your situation is that both kinds of your requests originate from the same QObject. 您遇到的问题是两种请求都来自同一个QObject。 You could create two dummy QObjects just for the purpose of sending their adresses to mark requests or you could use reinterpret_cast to convey kind of a request as a pointer. 您可以创建两个虚拟QObject,仅用于将其地址发送到标记请求,或者您可以使用reinterpret_cast将请求类型作为指针传达。
  4. Keep track of initiated requests yourself as you suggested. 按照您的建议自行跟踪已启动的请求。 Because there can be any number of requests active in one time you would have to use two lists of pointers (one for each kind of requests) for example 因为一次可以有任意数量的请求,所以你必须使用两个指针列表(每种请求一个),例如

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM