How to convert QString to C String (char *)

The best solution for this would be to go through standard C++ and it means this:

Let’s say you have a QString, like this:

QString s = "www.amin-ahmadi.com";

First convert it to std::string and then use its c_str method, like this:

s.toStdString().c_str()

2 Replies to “How to convert QString to C String (char *)”

  1. I think this post can get you in trouble. I have found that with large strings it seems like the std string can end up a dangling pointer before it gets converted to a cstring. This is even one of those bugs that will show up in production code but not when running from the IDE. Here is a post about it: https://stackoverflow.com/questions/6208565/const-char-vs-char-c and here is my fix:

    const std::string& stdS = s.toStdString();
    stdS.c_str()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.