如何在Qt中传递值?
我正在做NFC项目。在此,我需要检测标签并从 NFC 标签读取标签内容,并且需要将标签 ID 发送到 PHP 服务器。从服务器端我将得到作为 {tagId&tagcontent} 的响应。之后我需要提取 tagId 和 tagcontents。到目前为止,我已经完成并且工作正常。现在我的问题是将值传递给另一个类。一旦我从服务器收到 SUCCESS 响应,我就需要将提取的值传递给另一个类。我该怎么办呢。我是 Qt 编程新手。请帮我。
I am doing the NFC project. In this I need to detect the tag and read the tag contents from the NFC tag and i need to send the tag id to the PHP server. From the server side i will get the response as {tagId&tagcontent}. After this i need to extract the tagId and tagcontents. Till this i have finished and working fine. Now my problem is to pass the value to another class. As soon as i get the SUCCESS response from server i need to pass the extracted values to another class. How can i do that. I am new to Qt programming. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将参数作为键值对传递,即使用 QHash 或 QVector 来实现此目的。
此外,如果您希望信息过于具体,您可以定义一个自定义 C++ 类,其中包含响应字段的值,并将引用从第一个类传递到另一个类。
Qt 中的参数传递与 C++ 类似。
你的 QStringList、QVector 或 QHash 都只是类。
您可以像传递任何其他自定义类一样传递这些类的对象。
例如:
在您的目标类中(例如 T):
在您的调用类中(例如 C):
更具体地说,您可以通过值或引用传递 C++ 中的参数。
对于传递对象,最好通过引用传递它们。
按值传递意味着在被调用者的堆栈上创建对象的副本,而更改对象意味着更改本地副本,以便在函数返回时调用者的对象保持不变。通过引用传递意味着发送对象的地址(引用保存地址但行为类似于对象),以便被调用者可以直接更改原始对象。
看看这个线程有什么区别通过引用传递与通过值传递之间?
代码论坛上提供了很好的解释和示例,网址为 http://www.codeguru.com/forum/showthread.php?t=343473< /a>
You can pass the parameters as key value pairs i.e. use a QHash or a QVector for this purpose.
Moreover, if you want the information ot be too specific, you can define a custom C++ class, containing values for the response fields and pass the reference from your 1st class to the other class.
Parameter passing in Qt is similar to C++.
Your QStringList, QVector or QHash all are just classes.
You can pass objects of these class as any other custom class.
For example:
In your target class (say T):
In your calling class(say C):
More specifically, you can pass parameters inC++ either by value or by reference.
For passing objects, it is better to pass them via reference.
Passing by value means that a copy of the object is made on the callee’s stack and altering the object means altering a local copy so the caller’s object is unchanged when the function returns. Passing by reference means that the address of the object is send (a reference holds an address but behaves like an object) so that the callee can directly alter the original object.
Have a look at this thread for What's the difference between passing by reference vs. passing by value?
A nice explanation with examples is provided on codeforum at http://www.codeguru.com/forum/showthread.php?t=343473