News:

As usual while waiting for the next release - don't forget to check the nightly builds in the forum.

Main Menu

wxString to std::string question

Started by manmach, August 31, 2006, 09:37:19 AM

Previous topic - Next topic

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:

Quoteerror: 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.

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


QuoteI 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.
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)
#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)


And in your code, whenever your API is requiring a const char*, do this:


const wxCharBuffer pbuff = _C(wx_string); // convert wxString to wxCharBuffer
foo(pbuff.data()); // get the data as cnost char*


Eran

Pecan

#4
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/

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.

eranif

QuoteThere's got to be a better way to get an answer about wxWidgets than shadonet.

The best way is to register to the wxWidgets mailing list (not the developers)

VZ (Vadim Zeitlin) will probably answer your question :wink:, this guy is a machine gun when it comes to answers questions related to wxwidgets

Eran

Game_Ender

#6
Pecan exzagerated, not all questions get answered, but I would say at least half of them do and almost all get a response (just go look for yourself).  Bad mouthing and not going to a place that is trying to be the official wxWidgets forum will do nothing for its growth or usability.

EDIT, I removed the word bad from the sentence above, I don't know why its there.

tiwag

Quote from: Game_Ender on August 31, 2006, 07:43:52 PM
Pecan exzagerated, not all bad questions get answered, but I would say at least half of them do and almost all get a response ...

are you exaggerating ?  :)

Game_Ender

Sorry about that stray "bad" in there.  I don't think I am, you can browse the forums yourself the little check mark means the person who asked the question thinks they got an answer and I barely see any posts with "0" responses.