Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, March 2002
QString and latin1() question


Message 1 in thread

Hello All,

A search through the archives shows that this type of question has been asked 
many times before but I still can't see the answer to my problem.

I have a function that takes a parameter like this:
function(char "command");

The problem is that I have to built up the command string from a character 
array and a qstring along the following lines.

char string[25];
char string2 = {"some text "};

QString str = "here";

const char *string3 = str.latin1();

/* Build a command string */
sprintf(string,"%s %s","string2",string3);

If I now pass string to my function then the function fails (the function is 
part of a library and not my own code).

I notice that the kdevelop debugger shows string like this:
"some text here\000\000\000"

If I code something like this then there isn't a problem, string = {"some 
text here"}, and the debugger show this:
"some text here"

I have spent most of the day trying to sort this out and have tried may 
variations without a usable result. I'm sure that the answer is a simple one.

-- 
 [ signature omitted ] 

Message 2 in thread

> Hello All,
>
> A search through the archives shows that this type of question has been
asked
> many times before but I still can't see the answer to my problem.
>
> I have a function that takes a parameter like this:
> function(char "command");
>
Do you mean :
function(char * command);


> The problem is that I have to built up the command string from a character
> array and a qstring along the following lines.
>
> char string[25];
> char string2 = {"some text "};
>
Do you mean:
char *string2 = {"some text"};


> QString str = "here";
>
> const char *string3 = str.latin1();
>
> /* Build a command string */
> sprintf(string,"%s %s","string2",string3);
>
This should be OK.


> If I now pass string to my function then the function fails (the function
is
> part of a library and not my own code).
>
Put something like this as the first line of your function:

function(char *command)
{
    qDebug("command = \"%s\" command length = %d", command,
strlen(command));
    ...
}

It should help you to figure out if the problem is in the construction of
the command string, or in the implementation of the function.

--
 [ signature omitted ]