gpt4 book ai didi

c++ - 重构为独立类后,QNetworkRequest (HTTP GET) 不会触发

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:46 26 4
gpt4 key购买 nike

我最近开始了对大约 2 个月前编写的大型单片音频播放器应用程序进行模块化的繁琐过程。

这个过程进行得相当顺利,尽管其中一种方法(ScrobbleMedia - 可以预见的是它会发出 HTTP 请求以将有关播放轨道的信息提交到 last.fm)似乎不再发出网络请求。

但是,正在正确构建将传递给 QNetworkAccessManager 实例/QNetworkRequest 的 QUrl 对象。

为了比较,代码的功能性 Mercurial 修订版可在 BitBucket 上获得。 .

ScrobbleMedia 方法在重构后目前看起来像这样:

#include "scrobblemedia.h"

#include <QDebug>
#include <cstdio>

ScrobbleMedia::ScrobbleMedia(QString asUsername, QString asPassword,
QString asArtist, QString asTrack, QString asAlbum)
{

QString KEndPointURL = "http://lastfmstats.livefrombmore.com/universalscrobbler/scrobble.php";
QUrl iScrobbleEndPoint(KEndPointURL);

QNetworkAccessManager *iScrobbleDispatcher = new QNetworkAccessManager(this);


iScrobbleEndPoint.addQueryItem("submissionType","track");
iScrobbleEndPoint.addQueryItem("username", asUsername);
iScrobbleEndPoint.addQueryItem("password", asPassword);
iScrobbleEndPoint.addQueryItem("artist", asArtist);
iScrobbleEndPoint.addQueryItem("track", asTrack);
iScrobbleEndPoint.addQueryItem("album", asAlbum);
iScrobbleEndPoint.addQueryItem("number","1");
iScrobbleEndPoint.addQueryItem("duration","200");

iScrobbleDispatcher->get(QNetworkRequest(iScrobbleEndPoint));
connect(iScrobbleDispatcher, SIGNAL(finished(QNetworkReply*)),
SLOT(replyFinished(QNetworkReply*)));

// QString Outside = iScrobbleEndPoint.toEncoded();

qDebug() << "Received: " +
asUsername + " " +
asPassword + " " +
asArtist + " " +
asTrack + " " +
asAlbum;

qDebug() << iScrobbleEndPoint.toString();

}

ScrobbleMedia::~ScrobbleMedia() {

}

关联的头文件如下所示:

#ifndef SCROBBLEMEDIA_H
#define SCROBBLEMEDIA_H

#include <QString>
#include <QtNetwork>
#include <QUrl>
#include <QNetworkAccessManager>



class ScrobbleMedia : public QObject
{
Q_OBJECT;


private:

public:

ScrobbleMedia(QString asUsername, QString asPassword, QString asArtist, QString asTrack, QString asAlbum);
~ScrobbleMedia();

};

#endif // SCROBBLEMEDIA_H

我目前正在 Windows 7 x86-64 下针对 Qt 库的 4.7.0 版 MinGW 构建(作为 Qt SDK 2010.05 的一部分包含在内)构建应用程序本身。

如有任何帮助,我们将不胜感激。

提前致谢。

最佳答案

在阅读了许多不同的信息来源(其中大部分是相互矛盾的)之后,我发现以下结果是一个有效的解决方案——尽管调试输出中有一个似乎不影响操作的外观问题(Object::connect: No such signal QNetworkReplyImpl::finished(QNetworkReply*) in ../AudioPlayer/scrobblemedia.cpp:29):

scrobblemedia.cpp 中:

#include "scrobblemedia.h"

#include <QDebug>
#include <cstdio>

ScrobbleMedia::ScrobbleMedia(QString asUsername, QString asPassword,
QString asArtist, QString asTrack, QString asAlbum)
{

QByteArray iDataSink;
QEventLoop iLoop;

QString KEndPointURL = "http://lastfmstats.livefrombmore.com/universalscrobbler/scrobble.php";
QUrl iScrobbleEndPoint(KEndPointURL);

iScrobbleEndPoint.addQueryItem("submissionType","track");
iScrobbleEndPoint.addQueryItem("username", asUsername);
iScrobbleEndPoint.addQueryItem("password", asPassword);
iScrobbleEndPoint.addQueryItem("artist", asArtist);
iScrobbleEndPoint.addQueryItem("track", asTrack);
iScrobbleEndPoint.addQueryItem("album", asAlbum);
iScrobbleEndPoint.addQueryItem("number","1");
iScrobbleEndPoint.addQueryItem("duration","200");

QNetworkAccessManager iScrobbleDispatcher;
QNetworkRequest iScrobbleRequest(iScrobbleEndPoint);
QNetworkReply *iScrobbleReply = iScrobbleDispatcher.get(iScrobbleRequest);

QObject::connect(iScrobbleReply, SIGNAL(finished(QNetworkReply*)), &iLoop,
SLOT(quit()));

iDataSink = iScrobbleReply->readAll();

qDebug() << "Received: " + asUsername + " " + asPassword + " " + asArtist + " " + asTrack + " " + asAlbum;

qDebug() << iScrobbleEndPoint.toString();

iLoop.exec();
}

ScrobbleMedia::~ScrobbleMedia() {

}

void ScrobbleMedia::replyFinished(QNetworkReply*) {

}

void ScrobbleMedia::reallyDone() {

qDebug() << "We've probably successfully Scrobbled...";
}

scrobblemedia.h 中:

#ifndef SCROBBLEMEDIA_H
#define SCROBBLEMEDIA_H

#include <QString>
#include <QtNetwork>
#include <QUrl>
#include <QNetworkAccessManager>



class ScrobbleMedia : public QObject
{
Q_OBJECT


private:


public:

ScrobbleMedia(QString asUsername, QString asPassword, QString asArtist, QString asTrack, QString asAlbum);
~ScrobbleMedia();

private slots:
void replyFinished(QNetworkReply*);
void reallyDone();

};

#endif // SCROBBLEMEDIA_H

感谢大家的帮助。

希望这段代码将来能成为其他人的有用模板。

关于c++ - 重构为独立类后,QNetworkRequest (HTTP GET) 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4922664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com