Qt convert cFilename to type QString for creating a Qsound object
-
Hello,
I have a path stored in cFilename object, and iam trying to create a QSound Object, but while consructing the object i have give the path name as argument or during using play function i have to give the path name, and QSound takes only of type QString. How do i convert the cFilename object to of type QString? And this path has a pointer which points to a share directory of the folder.
If there is an any alternate solution please let me know
Thank you -
@VInay123 Hi! The answer PiotrNycz gave you on stackoverflow looks promising.
-
Hi,
When talking about non Qt types it's crucial to say what type they are, not just their name, otherwise it's like asking "how can I convert foo to QObject" without saying what foo is.
Anyway, because the name is often used in Win32 family of APIs, I'm guessing that
cFilename
is a member of a file information record, for example this one. Is that a correct guess?If so, then there are two possible answers to this question, and they depend on whether or not you have defined UNICODE in your app (most probably yes, even if not knowingly, because that's the default these days). That's because
cFilename
is of typeTCHAR
that can map to different underlying types depending on that define.If UNICODE is defined, then the
cFileName
is of typewchar_t[]
and (on Windows) it's a UTF-16 string. To convert that toQString
you'd use:QString foo = QString::fromWCharArray(your_file_descriptor.cFileName);
If UNICODE is not defined then the
cFilename
member is of typechar[]
and it's an ANSI string. For that you can use:QString foo = QString::fromLatin1(your_file_descriptor.cFileName);