2

I have a path stored in cFilename object, and Iam trying to create a QSound Object, but while constructing 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

  • What is type of your cFilename object? Can you get a 'plain' string from it ?(i.e. does it have something like c_str() method?) – mvidelgauz Jun 18 '16 at 09:41
  • As a step towards QString - use conversion to std::string: http://stackoverflow.com/questions/16195321/cannot-convert-findfiledata-cfilename-to-string – PiotrNycz Jun 18 '16 at 09:42
  • how do i convert cstring to qstring? – Vinay Balaji Rajputh Jun 18 '16 at 09:47
  • 1
    MFC CString? QString has a bunch of static fromXXX functions, just open QString header file and look which one appropriate. Be careful, CString itself depends on the compiling environment also. – douyw Jun 18 '16 at 12:03

2 Answers2

1

You need two steps:

1. CString to std::string:

CString cString("Hello");
CT2CA convertedString(cstring);
std::string stdString(convertedString);


2. std::string to QString:

QString qtString = QString::fromStdString(stdString);
0

You don't mention the type of cFilename, but if is std::string you could do QString::fromUtf8(cFilename.c_str()). If it is a const char* then you could do QString::fromUtf8(cFilename).

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70