C++ QNetworkAccessManager (Qt) 与 openGL 结合使用
我对 C++ 真的很菜鸟(正如我之前的帖子提到的),但是我的朋友建议我使用 QNetworkAccessManager,如果我想发送 HTTP GET 请求来发送信息。
我目前正在使用 openGL-es 并想要执行以下两行代码来发送 get 请求:
QNetworkAccessManager* netMan = new QNetworkAccessManager(this);
netMan->get(QNetworkRequest(QUrl("something/?userID=1")));
但是,它不喜欢“this”,因为它位于 main() 方法中并且不引用 QObject (我猜是 QApplication)。当我摆脱“this”时,我的应用程序会构建,但永远不会加载(我在顶部放置了一个“printf(1)”,它甚至不运行)。
关于如何解决这个问题有什么建议或替代方案吗?提前致谢。
——詹姆斯
I'm really nooby with C++ (as my previous posts mention), however my friend suggested I work with QNetworkAccessManager if I want to send a HTTP GET request to send information.
I am currently working with openGL-es and want to do the following two lines of code to send the get request:
QNetworkAccessManager* netMan = new QNetworkAccessManager(this);
netMan->get(QNetworkRequest(QUrl("something/?userID=1")));
However, it does not like the "this" because it is in the main() method and it does not reference a QObject (I'm guessing QApplication). When I get rid of the "this" my application builds, but just never loads (I put a "printf(1)" at the top which doesn't even run).
Any suggestions or alternatives on how to fix this? Thanks in advance.
-James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
QNetworkAccessManager
构造函数中的参数仅需要指定基于QObject
的父级,该父级将负责稍后清理(删除)您的对象,如果您计划这样做,则不需要自己调用delete
。我不太确定您所说的“从不加载”是什么意思,或者您在哪里放置了
printf
但为了取回任何内容,您需要实际保留QNetworkReply
> 调用get()
返回的指针。为了从中获得任何东西,您需要运行一个事件循环。如果您的应用程序仅是控制台(无 GUI),则可以使用
QCoreApplication
对象。尝试这个最小的代码:
The parameter in the
QNetworkAccessManager
constructor is only needed to specify aQObject
based parent which will be responsible for cleaning up (deleting) your object later and isn't necessary if you plan to calldelete
on it yourself.I'm not quite sure what you are referring to by "never loads" or where you put a
printf
but in order to get anything back, you need to actually keep theQNetworkReply
pointer that is returned by the call toget()
.And to get anything from that, you need an event loop running. If your application is console only (no GUI), you can use a
QCoreApplication
object.Try this minimal code: