Sergey Proskurnya
Sergey Proskurnya

Reputation: 59

QUrl parsing in QT 5

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?

Upvotes: 2

Views: 6208

Answers (3)

Juan
Juan

Reputation: 830

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"];
}

Upvotes: 0

Petr Tripolsky
Petr Tripolsky

Reputation: 1613

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

Upvotes: 0

Jablonski
Jablonski

Reputation: 18504

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

Upvotes: 5

Related Questions