QUrl parsing in QT 5 - c++

I have a QUrl as this:
https://www.example.com/index.html#token=SomeToken&user=guest
and I want to obtain the value of the token i.e. SomeToken. I know about method QUrl::queryItemValue,so this code must work:
void MainWindow::get_token(QUrl url)
{
url = url.toString().replace("?","#");
QString token = url.queryItemValue("token");
}
but in Qt5 i can't use this method,how can I parse url?

There is new QUrlQuery class in Qt5. New QUrl doesn't support this method yet, so you should use QUrlQuery for parsing (it has this and other methods). Use
QUrlQuery query(url);
qDebug() << query.queryItemValue("token");
Note: be carefull with replace because QUrlQuery gives you correct result with
?token=SomeToken not a #token=SomeToken
http://qt-project.org/doc/qt-5/qurlquery.html

QUrlQuery queryItemValue method does not work properly in Qt 5.9 So i wrote my own function to parse GET parameters
#include <QCoreApplication>
#include <QUrlQuery>
#include <QDebug>
#include <QMap>
#include <QUrl>
QMap<QString,QString> ParseUrlParameters(QString &url)
{
QMap<QString,QString> ret;
if(url.indexOf('?')==-1)
{
return ret;
}
QString tmp = url.right(url.length()-url.indexOf('?')-1);
QStringList paramlist = tmp.split('&');
for(int i=0;i<paramlist.count();i++)
{
QStringList paramarg = paramlist.at(i).split('=');
ret.insert(paramarg.at(0),paramarg.at(1));
}
return ret;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString url = "http://test1.ru/?token=test&email=test1";
QUrlQuery query(url);
qDebug() << "queryItemValue does not work in Qt 5.9.0 with dynamic QString" << query.queryItemValue("token") << "("<< endl;
qDebug() << "ParseUrlParameters(...) works fine..."<< endl;
QMapIterator<QString, QString> i(ParseUrlParameters(url));
while (i.hasNext())
{
i.next();
qDebug() << i.key() << ":" << i.value();
}
return a.exec();
}

I know this post is old but if my answer can help someone, I share :)
Tested under QT 5.15.2 and under QT 6.4.2
#include<QUrl>
#include<QUrlQuery>
#include<QDebug>
int main (int nbArg, char* listArg[])
{
// Initialization
QString myString = "https://www.factice.fr/demo.php?thing=123&subject=456&artificial=789";
QUrl myUrl(myString);
QUrlQuery myQuery(myUrl);
QMap<QString,QString> paramList; // Associative Array to Store Keys and Values
// For Each QPair
for(int i=0;i<myQuery.queryItems().size();i++)
{
// Information Display
qDebug() << myQuery.queryItems().at(i).first << " : " << myQuery.queryItems().at(i).second;
// Or Storage of Information for futur use
paramList.insert(myQuery.queryItems().at(i).first,myQuery.queryItems().at(i).second);
}
// End - For Each QPair
// Examples of Displaying Stored Information
qDebug() << paramList;
qDebug() << paramList["thing"];
}

Related

Qt 6.2: QMediaPlayer & QByteArray

Good day.
Has anyone tried QMediaPlayer in Qt 6.2 already?
I'm trying this code, but Media Status always remains as "NoMedia" and no any sound :).
Full test project: https://github.com/avttrue/MediaPlayerTest
#include "mainwindow.h"
#include <QDebug>
#include <QBuffer>
#include <QFile>
#include <QAudioOutput>
#include <QMediaPlayer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QFile file("../test/Bankrobber.mp3");
if(!file.open(QIODevice::ReadOnly))
qDebug() << "File not opened";
qDebug() << "File size:" << file.size(); // File size: 11181085
QByteArray ba = file.readAll();
qDebug() << "ByteArray size:" << ba.size(); // ByteArray size: 11181085
QBuffer* buffer = new QBuffer(this);
buffer->setData(ba);
if(!buffer->open(QIODevice::ReadOnly))
qDebug() << "Buffer not opened";
qDebug() << "Buffer size:" << buffer->size(); // Buffer size: 11181085
buffer->seek(qint64(0));
auto audioOutput = new QAudioOutput(this);
auto player = new QMediaPlayer(this);
player->setAudioOutput(audioOutput);
audioOutput->setVolume(50);
player->setSourceDevice(buffer);
qDebug() << "Device:" << player->sourceDevice(); // Device: QBuffer(0x563180493020)
QObject::connect(player, &QMediaPlayer::mediaStatusChanged,
[=](QMediaPlayer::MediaStatus status)
{ qDebug() << "MediaStatus:" << player->mediaStatus() << "|" << status; });
QObject::connect(player, &QMediaPlayer::errorOccurred,
[=](QMediaPlayer::Error error)
{ qDebug() << "Error:" << player->errorString() << "|" << error; });
QObject::connect(player, &QMediaPlayer::playbackStateChanged,
[=](QMediaPlayer::PlaybackState state)
{ qDebug() << "PlaybackState:" << player->playbackState() << "|" << state; });
player->play();
qDebug() << "MediaStatus:" << player->mediaStatus(); // MediaStatus: QMediaPlayer::NoMedia
}
As the docs points out:
void QMediaPlayer::setSourceDevice(QIODevice *device, const QUrl
&sourceUrl = QUrl())
Sets the current source device.
The media data will be read from device. The sourceUrl can be provided
to resolve additional information about the media, mime type etc. The
device must be open and readable.
For macOS the device should also be seek-able.
Note: This function returns immediately after recording the specified
source of the media. It does not wait for the media to finish loading
and does not check for errors. Listen for the mediaStatusChanged() and
error() signals to be notified when the media is loaded, and if an
error occurs during loading.
(emphasis mine)
QMediaPlayer does not know how to deduce the file format so it does not load it. The solution is to point out that it is an mp3:
player->setSourceDevice(buffer, QUrl("foo.mp3"));
The function setSourceDevice() you are using isn't doing what you think?
Maybe you wanted setSource() instead?
Qt has great documentation:
https://doc.qt.io/qt-6/qmediaplayer.html#setSourceDevice
Even good examples:
player = new QMediaPlayer;
audioOutput = new QAudioOutput;
player->setAudioOutput(audioOutput);
connect(player, SIGNAL(positionChanged(qint64)), this, SLOT(positionChanged(qint64)));
player->setSource(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3"));
audioOutput->setVolume(50);
player->play();
Ref.
https://doc.qt.io/qt-6/qmediaplayer.html#details
May be this is as variant, but i think it not good:
QTemporaryFile tfile;
if (!tfile.open())
qDebug() << "TemporaryFile not opened";
else
{
qDebug() << "TemporaryFile writed:" << tfile.write(ba);
if(tfile.size() != ba.size())
qDebug() << "TemporaryFile not complited";
else
player->setSource(QUrl::fromLocalFile(tfile.fileName()));
}

QT: manage the reply from a QNetworkReply

I'm new in QT development and I've to make a non-gui application that reads a token from a POST request and then launches some json requests using that token. My problem is what to do when the finished signal is launched. I've tried to pass the reply.readAll() to a QByteArray parameter of the object, but when I do this the value is always empty (""). My code was done based on this.
.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QNetworkAccessManager>
class QNetworkReply;
class Downloader : public QObject
{
Q_OBJECT
public:
Downloader(QObject* parent=0);
void test();
bool finished = false;
QByteArray data;
public slots:
void handleReply(QNetworkReply* reply);
protected:
QNetworkAccessManager m_manager;
};
#endif // DOWNLOADER_H
.cpp
#include "downloader.h"
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QVariant>
#include <QDebug>
Downloader::Downloader(QObject *parent) :
QObject(parent)
{
connect(&m_manager, SIGNAL(finished(QNetworkReply*)), SLOT(handleReply(QNetworkReply*)));
}
void Downloader::test() {
QNetworkRequest request;
QUrl url("http://192.168.25.25:8080/path/to/token");
QUrl postData;
postData.addQueryItem("client_id", "foo");
postData.addQueryItem("username", "bar");
postData.addQueryItem("password", "12345");
postData.addQueryItem("grant_type", "password");
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
QNetworkReply* rep = m_manager.post(request,postData.encodedQuery());
rep->setProperty("url", QVariant(url));
qDebug() << "Post " << rep << rep->url();
}
void Downloader::handleReply(QNetworkReply *reply) {
qDebug() << "Handle" << reply << reply->url();
qDebug() << "Dynamic property" << reply->property("url").isValid() << reply->property("url");
qDebug() << "ReadAll " << reply->readAll();
finished = true;
data =reply->readAll();
reply->deleteLater();
}
In main, the call is:
Downloader down;
down.test();
while (!down.finished)
{
usleep(3*1000*1100);//3,3s
cout << "no finalizado";
}
What I'm trying to do is to use the reply to fill a parameter and use this parameter from the main, when the finished bool is true. I know it's not correct, but I don't know how to manage the asynchronous nature of the request. What I need it's some guide to understand what I'm doing, since I've been searching in qt page, stackoverflow and others without success. Thanks in advance.
Update: my main function
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Downloader down;
down.test();
return a.exec();
}
In class Downloader, declare a signal to emit the data to a slot of another QObject derived class. For example, in Downloader::handleReply you call emit dataReady(data). In the main function add below code:
Downloader down;
JsonSender obj;
connect(&down, &Downloader::dataReady, &obj, &JsonSender::dataReady);
down.test();
return a.exec();
In the slot dataReady of class JsonSender you can parse the token and send the request.

Delete Key/Value Pair from configuration file qsetting

i am trying to search String in configuration file and if string match wants to delete key / value pair. i have getting qstringlist from file .
as far as my tried code is
int main(int argc, char *argv[])
{
QSettings* settings= new QSettings("/home/sidheshwar/Desktop/temp.txt", QSettings::IniFormat);
settings->beginGroup("Profiles");
const QStringList childKeys = settings->childKeys();
QStringList Keys;
QStringList values;
QString user="db-host";
QString tempUser;
foreach (const QString &childKey, childKeys)
{
Keys << childKey;
values << settings->value(childKey).toString();
}
for(int i=0;i< Keys.length();i++){
if(user == values.at(i)){
qDebug() << " keys" << Keys[i] << endl;
tempUser=Keys[i];
}
qDebug() << " tempUser" << tempUser << endl;
}
return 0;}
how can i use settings->remove(tempUser);
In the following example I show you an example of how to delete a data from the file that handles the configuration.
temp.ini before the execution.
[Profiles]
key1=db-host
key2=value2
key3=value3
main.cpp
#include <QCoreApplication>
#include <QSettings>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSettings* settings= new QSettings("temp.ini", QSettings::IniFormat);
settings->beginGroup("Profiles");
const QStringList childKeys = settings->childKeys();
QStringList Keys;
QStringList values;
QString user="db-host";
foreach (const QString &childKey, childKeys)
{
Keys << childKey;
values << settings->value(childKey).toString();
}
for(int i=0;i< Keys.length();i++){
if(user == values.at(i)){
qDebug() << " keys" << Keys[i];
settings->remove(Keys[i]);
}
qDebug() << Keys[i] << values.at(i);
}
return a.exec();
}
Output:
temp.ini after the execution
[Profiles]
key2=value2
key3=value3

Using QNetworkAccessManager->Post() causes SEGV on closing the application

****UPDATED: I noticed that I get the segfault only on Windows, on Linux it's fine. On Windows I use QT 5.5 and MinGW32. I still want to know why.
**** Initial Question:
Nothing tricky here, I create a basic Console Application. I have a QNetworkAccessManager sending a Post() request. When I close the console, there is a segfault.
Note that the request is sent and received successfully, my question is only about that segfault.
If no Post() request are sent, no crash on closing the console. There is not much help from the stack.
Stack
0 ntdll!RtlFreeHeap 0x77b5e041
1 ucrtbase!free 0x5e4c5eab
2 LIBEAY32!CRYPTO_free 0x5e5a123e
Main.cpp
#include <QCoreApplication>
#include "CNetworkHandleTest.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
CNetworkHandleTest net;
net.start();
return a.exec();
}
CNetworkHandleTest.cpp
#include "CNetworkHandleTest.h"
CNetworkHandleTest::CNetworkHandleTest()
{
m_Manager = new QNetworkAccessManager(this);
// Connect the network manager so we can handle the reply
connect(m_Manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
m_nTotalBytes = 0;
}
CNetworkHandleTest::~CNetworkHandleTest()
{
disconnect();
m_Manager->deleteLater();
}
void CNetworkHandleTest::onFinished(QNetworkReply* reply)
{
// Look at reply error
// Called when all the data is receivedqDebug() << "Error code:" << reply->error();
qDebug() << "Error string:" << reply->errorString();
reply->close();
reply->deleteLater();
}
void CNetworkHandleTest::start()
{
// Configure the URL string and then set the URL
QString sUrl(BASE_URL);
sUrl.append("/console/5555/upload");
m_Url.setUrl(sUrl);
// Make the request object based on our URL
QNetworkRequest request(m_Url);
// Set request header (not sure how or why this works, but it works)
// \todo investigate
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlenconded");
// Make file object associated with our DB file
QFile file("/tx_database.db");
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "Failed to open file";
}
// Read the entire file as a binary blob
QByteArray data(file.readAll());
// Set our request to our request object
// Note: there should probably be a flag so that when start is called it does not do
// any processing in case we are already in the middle of processing a request
m_Request = request;
// Send it
m_Reply = m_Manager->post(m_Request, data);
// Need to connect the signals and slots to the new reply object (manager makes a new
// reply object every post; may need to investigate if memory should be freed when
// done processing a response)
connect(m_Reply, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(m_Manager, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
this, SLOT(onAuthenticationRequired(QNetworkReply*, QAuthenticator*)));
}
void CNetworkHandleTest::onReadyRead()
{
// Whenever data becomes available, this slot is called. It is called every time data
// is available, not when all the data has been received. It is our responsibility to
// keep track of how much we have received if we want to show progress or whatever
// but we do not need to keep track if we have received all the data. The slot
// OnFinished will be called when the all the data has been received.
qDebug() << "Bytes available:" << m_Reply->bytesAvailable();
m_nTotalBytes += m_Reply->bytesAvailable();
qDebug() << "Bytes thus far:" << m_nTotalBytes;
QByteArray responseData = m_Reply->readAll();
qDebug() << "Response" << responseData;
m_Reply->size();
}
void CNetworkHandleTest::onAuthenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
{
}
CNetworkHandleTest.h
#ifndef CNETWORKHANDLETEST_H
#define CNETWORKHANDLETEST_H
// Required packages
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QByteArray>
#include <QUrlQuery>
#include <QHostInfo>
#include <QObject>
#include <QUrl>
// Helper packages
#include <QCoreApplication>
#include <QFile>
#include <QDir>
// Our packages
#include <iostream>
#include <fstream>
#include <cstdlib>
#define BASE_URL "localhost:5000"
#define BOUNDARY "123456787654321"
class CNetworkHandleTest : public QObject
{
Q_OBJECT
public:
CNetworkHandleTest();
~CNetworkHandleTest();
void start();
protected Q_SLOTS:
void onFinished(QNetworkReply* reply);
void onReadyRead();
void onAuthenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator);
private:
QNetworkAccessManager* m_Manager;
QNetworkRequest m_Request;
QNetworkReply* m_Reply;
QUrl m_Url;
int m_nTotalBytes;
};
#endif // CNETWORKHANDLETEST_H
When you close the console, your program dies in a most ungraceful manner. You need to write some code to make it graceful instead:
The below is a complete test case:
// https://github.com/KubaO/stackoverflown/tree/master/questions/network-cleanup-40695076
#include <QtNetwork>
#include <windows.h>
extern "C" BOOL WINAPI handler(DWORD)
{
qDebug() << "bye world";
qApp->quit();
return TRUE;
}
int main(int argc, char *argv[])
{
SetConsoleCtrlHandler(&handler, TRUE);
QCoreApplication a(argc, argv);
QNetworkAccessManager mgr;
int totalBytes = 0;
QObject::connect(&mgr, &QNetworkAccessManager::finished, [](QNetworkReply *reply){
qDebug() << "Error string:" << reply->errorString();
});
QNetworkRequest request(QUrl{"http://www.google.com"});
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlenconded");
auto reply = mgr.post(request, QByteArray{"abcdefgh"});
QObject::connect(reply, &QIODevice::readyRead, [&]{
qDebug() << "Bytes available:" << reply->bytesAvailable();
totalBytes += reply->bytesAvailable();
qDebug() << "Bytes thus far:" << totalBytes;
reply->readAll();
});
QObject::connect(reply, &QObject::destroyed, []{
qDebug() << "reply gone";
});
QObject::connect(&mgr, &QObject::destroyed, []{
qDebug() << "manager gone";
});
return a.exec();
}
If you press Ctrl-C or click [x] on the console window, the shutdown is orderly, the output being:
[...]
bye world
reply gone
manager gone

Qt - Writing integer data into JSON

I am using Qt (5.5) and I want to exchange data in JSON format in a client-server application.
So the format is constant:
{
"ball":
{
"posx": 12,
"posy": 35
}
}
I would like to be able to define a ByteArray or string like so:
QByteArray data = "{\"ball\":{\"posx\":%s,\"posy\":%s}}"
and then just write whatever the values for that into the string.
How do I do that?
QtJson is baked into Qt 5. It is easy to use, and gets it all ready for you pretty easily.
#include <QCoreApplication>
#include <QDebug>
#include <QJsonObject>
#include <QJsonDocument>
void saveToJson(QJsonObject & json);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QJsonObject jsonObject;
saveToJson(jsonObject);
QJsonDocument jsonDoc(jsonObject);
qDebug() << "Example of QJsonDocument::toJson() >>>";
qDebug() << jsonDoc.toJson();
qDebug() << "<<<";
return a.exec();
}
void saveToJson(QJsonObject & json)
{
QJsonObject ball;
ball["posx"] = 12;
ball["posy"] = 35;
json["ball"] = ball;
}
output
Example of QJsonDocument::toJson() >>>
"{
"ball": {
"posx": 12,
"posy": 35
}
}
"
<<<
Note: qDebug() wraps QString objects in quotes when printing. To get rid of that, pass your QString into qPrintable(). And it puts endl in for you at the end of each line.
For a more complex example see the official:
JSON Save Game Example
http://doc.qt.io/qt-5/qtcore-json-savegame-example.html
Hope that helps.
And here are more examples of string manipulations, but for readability and maintainability, please use the QJson classes.
QString str;
str = QString("{\"ball\":{\"posx\":%1,\"posy\":%2}}").arg(12).arg(35);
qDebug() << qPrintable(str);
QByteArray ba = str.toLocal8Bit();
qDebug() << ba;
QString str2;
str2 = "{\"ball\":{\"posx\":"
+ QString::number(12)
+ ",\"posy\":"
+ QString::number(35)
+ "}}";
qDebug() << qPrintable(str2);
output
{"ball":{"posx":12,"posy":35}}
"{"ball":{"posx":12,"posy":35}}"
{"ball":{"posx":12,"posy":35}}
Note again that the quotes are added by qDebug() when printing a QByteArray object.
Hope that helps.