How to insert value in URL?

I have the following code where I am trying to embed randomValuein a URL.

int randomValue = qrand() % 100;
view = new QWebView(this) ;

view->load(QUrl("http://blogsearch.google.com/blogsearch?q=C&start="+randomValue+"&num=1&output=rss"));

The following error is reported:

error: invalid operands of types 'const char *' and 'const char [18]' to binary 'operator +'

So, I want to add randomValueto the URL. How to do it?

+3
source share
5 answers

Use QString for this. It is much more efficient than std :: string, and it provides what you need directly.

QString baseurl("http://blogsearch.google.com/blogsearch?q=C&num=1&output=rss&start=%1");
view->load(QUrl(baseurl.arg(randomValue)));

See the QString documentation for more details .

+4
source

Use the function sprintf. See here for example.

- std::string std::ostringstream. , ( , QUrl).

char mytext[ 256 ]; // make sure the buffer is large enough!
int randomValue = 12345;
sprintf( "http://blogsearch.google.com/blogsearch?q=C&start=%d&num=1&output=rss", randomValue );

% d, . sprintf randomValue, . . .

: , Qt, .

+2

operator + char*. ++- std::string, qt-specific QString, operator +.

, QString - , QUrl .

+2
source

Use stringstream to create a string

std :: ostringstream oss; oss <" http://blogsearch.google.com/blogsearch?q=C&start= " <<randomValue <"& Amp; Num = 1 & exit = RSS"; view-> loads (QUrl (oss.str ()));

0
source

using

... + QString::number( randomValue ) + ...

and it should work.

Neither C ++ (nor QString) implies converting numbers to strings.

0
source

Source: https://habr.com/ru/post/1757307/


All Articles