User forums > General (but related to Code::Blocks)
wxString to std::string question
manmach:
I have been trying all kinds of keywords on Google to find out how I can convert a wxString to std::string, but have yet to find anything at all on the subject. Which is weird, because I would expect it to be fairly common if you want to use other libraries with wxWidgets.
So I am hoping at least one of you has this experience and is willing to share.
I need to take a url from a text control and pass it to a library that expects a std::string. I can get the url from the control using GetValue, but that gives me a wxString.
I have tried to use wxString::c_str(), but that gives me a compilation error:
--- Quote ---error: conversion from `const wxChar*' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested
--- End quote ---
I am using wxWidgets 2.6.3 unicode and Codeblocks with Mingw.
tiwag:
this forum is related to Code::Blocks,
please ask your wxWidgets related questions there
http://wxforum.shadonet.com/
Dahman:
std::string std_string = wx_string.c_str();
eranif:
--- Quote ---I need to take a url from a text control and pass it to a library that expects a std::string. I can get the url from the control using GetValue, but that gives me a wxString.
I have tried to use wxString::c_str(), but that gives me a compilation error:
Quote
error: conversion from `const wxChar*' to non-scalar type `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' requested
I am using wxWidgets 2.6.3 unicode and Codeblocks with Mingw.
--- End quote ---
The problem is that you are using unicode, and your API expects const char*.
You need to convert the string.
Here is a link to unicode:
http://wiki.codeblocks.org/index.php?title=Unicode_Standards
And in short, what you need is:
Add this section to one of your headers (a one that is included by all)
--- Code: ---#ifdef wxUSE_UNICODE
#define _U(x) wxString((x),wxConvUTF8)
#define _UU(x,y) wxString((x),y)
#define _CC(x,y) (x).mb_str((y))
#else
#define _U(x) (x)
#define _UU(x,y) (x)
#define _CC(x,y) (x)
#endif
#define _C(x) _CC((x),wxConvUTF8)
--- End code ---
And in your code, whenever your API is requiring a const char*, do this:
--- Code: ---const wxCharBuffer pbuff = _C(wx_string); // convert wxString to wxCharBuffer
foo(pbuff.data()); // get the data as cnost char*
--- End code ---
Eran
Pecan:
--- Quote from: tiwag on August 31, 2006, 10:29:06 am ---this forum is related to Code::Blocks,
please ask your wxWidgets related questions there
http://wxforum.shadonet.com/
--- End quote ---
Personally, I wouldn't recommend shadonet to anyone. For a year, I've left questions/fixes/suggestions etc. there, and have never received an answer. The majority of questions get 0 responses.
Plus, the developers stay on their mailing lists and never visit the users forum.
There's got to be a better way to get an answer about wxWidgets than shadonet.
Navigation
[0] Message Index
[#] Next page
Go to full version