I can not use char '&' Qt Creator

0

I'm trying to return a QString, which will be used as a URL and need to use '&' as a separator in the URL. However, when the character is placed in the QString, it breaks the URL and what should appear after the '&' does not appear.

retorno = "start http://localhost:5000/robo/?Nome="+nome+"&Capital="+capital;

In the case, 'name' and 'capital' are QStrings declared previously. I have already tried declaring a QChar with the '&' ASCII code and I have the same error.

    
asked by anonymous 07.08.2017 / 15:10

2 answers

2

This is because% com_and_company is used by the system to separate two commands on the same line, it is a "special" character, such as pipe ( & ) and parses ( | and ( ), and all can be escaped with )

However if you are using Qt, it already has functions ready for it, which prevents you having to have this job of escaping the characters, for example:

Using ^

Using QProcess with QStringList you can pass the arguments and the "Qt" itself will escape them to you:

retorno = "http://localhost:5000/robo/?Nome="+nome+"&Capital="+capital;

QProcess process;
process.start("start", QStringList() << retorno);

What you probably should already solve, however it is interesting to note that Qt itself has a class called QProcess

Using QDesktopServices

It already has the goal of interacting with OS functions, ie if you ported your program to Linux or OSX you will not need to change anything in this part (it will depend on what else you did), example with QDesktopServices (% is required):

retorno = "http://localhost:5000/robo/?Nome="+nome+"&Capital="+capital;

QDesktopServices::openUrl(QUrl(retorno));

So, it will fetch the system program that is associated with the URL protocol, in the case HTTP will probably open the default browser and also will not need escapes (like openUrl ). >     

08.11.2017 / 22:37
1

The problem was in the '&' character which was passed to the island. I put '^' before each '& of the QString and the console understood as a character instead of a command.

    
07.08.2017 / 16:25