c++qtappendqt5qurl

QUrlQuery append?


Is it possible to use QUrlQuery to append data without striping the url?

Using the code bellow will strip everything after the "?" and the result is:

https://foobar.com/Info.xml.aspx?userdata=1234

I would like to get:

https://foobar.com/Info.xml.aspx?user=jack&userdata=1234

QUrl url("https://foobar.com/Info.xml.aspx?user=jack&");
QString data = "1234"; 

QUrlQuery query;
query.addQueryItem("userdata", data);
url.setQuery(query); 

I'm asking because i need to make multiple calls, every time adding a new parameter and "building" the url from scratch every time is annoying.


Solution

  • You have to get the query and then add the item:

    QUrl url("https://foobar.com/Info.xml.aspx?user=jack&");
    
    QString data = "1234";
    QUrlQuery query(url.query());
    query.addQueryItem("userdata", data);
    url.setQuery(query);
    
    qDebug()<<url;
    

    Output:

    QUrl("https://foobar.com/Info.xml.aspx?user=jack&userdata=1234")