Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert from const char* to QString (or std::string to QString)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 25.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    justinlav23
    wrote on 30 Apr 2014, 01:01 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on 30 Apr 2014, 01:18 last edited by
      #2

      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);@

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • J Offline
        J Offline
        justinlav23
        wrote on 30 Apr 2014, 22:48 last edited by
        #3

        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!

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on 30 Apr 2014, 23:06 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on 30 Apr 2014, 23:20 last edited by
            #5

            [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...

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0

            1/5

            30 Apr 2014, 01:01

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved