Convert from const char* to QString (or std::string to QString)



  • So I have a qt project and I'm using an array with a struct. So my array has four different members, but the one I'm having trouble with is string name. my array is called advertisers so when i call name i call it as such: advertisers[i].name. I need to convert this string value to a Qstring and I have attempted to convert it to a const char* (which does work), but I can't seem to convert that to QString either. I have tried QString itemText = QString::fromStdString(name) and although that doesn't give me a compiler error, it is responsible for crashing my program.
    So what I currently have is:

    @
    const char* name = advertisers[i].name.c_str();
    QString itemText = QString::fromStdString(name);
    @

    Thanks so much, I greatly appreciate any help.



  • So is "advertisers[i].name" a std::string or a native C string?

    If it's a std::string, simply use:
    @QString itemText = QString::fromStdString(advertisers[i].name);@

    Otherwise, if it's a C string, use:
    @QString itemText = QString::fromUtf8(advertisers[i].name);@



  • That's the problem, I'm not quite sure. The struct for the advertisers array is declared with name simply being std::string, but when i use:

    @QString itemText = QString::fromStdString(advertisers[i].name);@

    the program crashes. And if I comment it out then everything works fine, so I know it is due to this line.

    I attempted the C string style as you suggested and i get the allowing error:
    "error: no viable conversion from 'std::string' (aka
    'basic_string<char, char_traits<char>, allocator<char> >') to
    'const char *'" so i'm pretty sure that's not the approach then.

    Thanks for the reply, any help is greatly appreciated!



  • Hi, are you sure it doesn't crash while accessing the array or somewhere else?
    You can separate the code or use a debugger to find out where exactly it crashes

    you can try this, if it keeps crashing and you can't use a debugger
    @
    auto ad = advertisers[i]; // maybe it already crashes here?
    std::string name = ad.name;
    QString itemText = QString::fromStdString(name);
    @
    I used "auto" (c++11) because i don't know the type of your struct.



  • [quote author="justinlav23" date="1398898092"]That's the problem, I'm not quite sure. The struct for the advertisers array is declared with name simply being std::string, but when i use:

    @QString itemText = QString::fromStdString(advertisers[i].name);@

    the program crashes. And if I comment it out then everything works fine, so I know it is due to this line. [/quote]

    If it's a std::string indeed, then the conversion via fromStdString() should be the proper way. But the reason for the crash is impossible to know without seeing your code, so we can only speculate! For example, it could be an array out of bounds access of the "advertisers[i]" array. Could also be that the "name" member is a reference (you didn't say what it is in detail), in which case it might be pointing to an object that doesn't exist anymore. And so on! So, as Xander84 suggested, you should analyze the crash with your Debugger...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.