2

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?

Jablonski
  • 18,083
  • 2
  • 46
  • 47

3 Answers3

5

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

Jablonski
  • 18,083
  • 2
  • 46
  • 47
0

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();
}

enter image description here

Petr Tripolsky
  • 1,419
  • 15
  • 23
  • 1
    To make `queryItemValue` work in new Qt versions I had to create `QUrlQuery` from a `QUrl` instead of from a string: `QUrlQuery query(QUrl(myurl));` – Aritz Mar 25 '21 at 13:46
0

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"];
}
Juan
  • 690
  • 4
  • 15