PDA

View Full Version : Character from argument disappears



Pepe
19th June 2007, 00:15
Using Qt 4.3 on Windows XP.

If I pass to my application from command line a filename (or any text) which contains the character ' (apostrophe), that character disappears. Example: "hello'goodbye" turns into "hellogoodbye".

The code I use is something like this:



QApplication a( argc, argv );
QString argument = QString::fromLocal8Bit( a.argv()[1] )


The same code on Linux with Qt 4.2.3 works ok, no character missing.

Am I doing something wrong? Or is it a bug?

patrik08
19th June 2007, 00:26
Using Qt 4.3 on Windows XP.
If I pass to my application from command line a filename (or any text) which contains the


QApplication a( argc, argv );
QString argument = QString::fromLocal8Bit( a.argv()[1] )




if you follow only the source code from http://wiki.qtcentre.org/index.php?title=Open_dokument_on_Mac_OSX_Finder QEvent::FileOpen
!not the xml is work on window & mac & linux ... minimal hack and work on my Xfce 4 manager...

wysota
19th June 2007, 00:49
Using Qt 4.3 on Windows XP.

If I pass to my application from command line a filename (or any text) which contains the character ' (apostrophe), that character disappears. Example: "hello'goodbye" turns into "hellogoodbye".

The shell probably eats up your apostrophe. Try surrounding the whole argument with quotes, like:

myapplication.exe "hello'goodbye"

Pepe
19th June 2007, 01:29
No, it doesn't work with or without quotes, the apostrophe is still missing.

But I've just found out something: if I enable the console ("CONFIG += console" in the pro file) then the problem fixes, the filename is read ok. But I can't leave the console, it's ugly (when the user open the application, a console opens too).

A Qt bug?

ChristianEhrlicher
19th June 2007, 06:53
As wysota already mentioned - the shell eats the apostrophe -> you have to quote it.


myApp.exe hello\'goodbye

Pepe
19th June 2007, 09:47
Ok, that way it works, with the slash in front of the apostrophe.

But the problem is that when the user double clicks on the file to open it, the filename is passed without the slash, so the file can't be opened!

wysota
19th June 2007, 11:03
Blaim the shell :) Using quotes should help as well, I don't know why it didn't... Are you sure you used them correctly?

ChristianEhrlicher
19th June 2007, 11:06
The quotes don't help here - just tested it out... really strange

Michiel
19th June 2007, 14:29
Ok, that way it works, with the slash in front of the apostrophe.

But the problem is that when the user double clicks on the file to open it, the filename is passed without the slash, so the file can't be opened!

Have you actually tested that? It is probable that it was just the shell that ate your apostrophe. Double-clicking on a file might just work.

Pepe
19th June 2007, 16:55
Of course I tested it. Double-clicking doesn't open the file, the apostrophe is lost.

But the strange thing, as I said before, is that if I enable the console (by adding "CONFIG += console" in the pro file) then the apostrophe is passed, and double-clicking works! Why?

wysota
19th June 2007, 17:05
Probably one of Windows quirks. Do file names containing apostrophes can be opened by other (non-Microsoft origin) applications?

Pepe
19th June 2007, 22:54
Yes, other programs can open such files. I tested with a file named "that's_all.odt" which is opened by open office without problems. Also winamp can open a "that's_all.mp3" file.

I've made a small program which can be used to test this problem. It simply displays the arguments in a QLabel:

main.cpp:


#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel label;

QString s;
for (int n= 0; n < a.argc(); n++) {
QString argument = QString::fromLocal8Bit( a.argv()[n] );
//QString argument = QString( a.argv()[n] );
s += QString("arg # %1: <b>%2</b><br>").arg(n).arg(argument);
}
label.setText(s);

label.show();
return a.exec();
}


test.pro:


SOURCES = main.cpp

#CONFIG += console


Running test.exe "that's all", displays as arg #1 "thats all".
test.exe "that\'s all" properly displays "that's all".

But right-clicking in the explorer on a filename with an apostrophe, and selecting to open it with test.exe displays the file without the apostrophe.

Removing the QString::fromLocal8Bit() call doesn't make any difference.
But uncommenting the #CONFIG += console line in the pro file magically fixes the problem.
Now test.exe "that's all" displays "that's all".

This is with Qt 4.3.0 in Windows. I haven't tested it with another version.

wysota
19th June 2007, 23:30
And what does QCoreApplication::arguments() return?

BTW. Qt doesn't have anything to do with the problem. argc and argv is populated by the system.

Pepe
19th June 2007, 23:48
I'll check QCoreApplication::arguments() tomorrow (right now I'm on linux, so I can't test it).

But what I did realize is that Qt 4.2.3 doesn't have this problem. A filename with an apostrophe does open in my application. So this seems to be a bug in Qt 4.3.0.

Edit: Tested QCoreApplication::arguments(). It does the same, the apostrophe disappears :(