C++ QNetworkAccessManager (Qt) 与 openGL 结合使用

发布于 2025-01-06 08:20:52 字数 475 浏览 1 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(1

短叹 2025-01-13 08:20:52

QNetworkAccessManager 构造函数中的参数仅需要指定基于 QObject 的父级,该父级将负责稍后清理(删除)您的对象,如果您计划这样做,则不需要自己调用delete

我不太确定您所说的“从不加载”是什么意思,或者您在哪里放置了 printf 但为了取回任何内容,您需要实际保留 QNetworkReply > 调用 get() 返回的指针。

为了从中获得任何东西,您需要运行一个事件循环。如果您的应用程序仅是控制台(无 GUI),则可以使用 QCoreApplication 对象。

尝试这个最小的代码:

#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QNetworkAccessManager *netMan = new QNetworkAccessManager();
    QNetworkReply *reply = netMan->get(QNetworkRequest(QUrl("http://google.com")));
    a.connect(reply, SIGNAL(finished()), SLOT(quit()));
    a.exec();
    qDebug() << reply->readAll();
    delete netMan;
}

The parameter in the QNetworkAccessManager constructor is only needed to specify a QObject based parent which will be responsible for cleaning up (deleting) your object later and isn't necessary if you plan to call delete 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 the QNetworkReply pointer that is returned by the call to get().

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:

#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QNetworkAccessManager *netMan = new QNetworkAccessManager();
    QNetworkReply *reply = netMan->get(QNetworkRequest(QUrl("http://google.com")));
    a.connect(reply, SIGNAL(finished()), SLOT(quit()));
    a.exec();
    qDebug() << reply->readAll();
    delete netMan;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文