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. Converting QString to const char* ?
Forum Updated to NodeBB v4.3 + New Features

Converting QString to const char* ?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 844 Views 2 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.
  • P Offline
    P Offline
    part-time roadman
    wrote on last edited by part-time roadman
    #1

    I need to convert QString to const char* reliably so I can use lua_pushstring() which only takes const char* as an argument. Is there a way to do this?

    thestring.toStdString().c_str()  /// Also doing this breaks the string and returns this unknown □ thing in place of the actual string.
    

    Or alternatively is there a way to display std::strings on UIs instead of QString?

    jsulmJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      @part-time-roadman said in Converting QString to const char* ?:

      thestring.toStdString().c_str() /// Also doing this breaks the string and returns this unknown □ thing in place of the actual string.

      You are taking the address of the first char of the temporary std::string returned by toStdString. Store the result of toStdString and then use that.

      You can convert your std::string to QString for the UI part.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • P part-time roadman

        I need to convert QString to const char* reliably so I can use lua_pushstring() which only takes const char* as an argument. Is there a way to do this?

        thestring.toStdString().c_str()  /// Also doing this breaks the string and returns this unknown □ thing in place of the actual string.
        

        Or alternatively is there a way to display std::strings on UIs instead of QString?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @part-time-roadman said in Converting QString to const char* ?:

        Or alternatively is there a way to display std::strings on UIs instead of QString?

        Which UIs?
        What is your use case actually?
        You can always convert std::string to QString.

        How to convert QString to char* depends on ecoding of your QString.
        Take a look at various QString::to*() methods.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • P Offline
          P Offline
          part-time roadman
          wrote on last edited by
          #4

          Alright how would I convert std::string to QString?

          jsulmJ 1 Reply Last reply
          0
          • P part-time roadman

            Alright how would I convert std::string to QString?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @part-time-roadman Easy to find in the documentation: https://doc.qt.io/qt-5/qstring.html#fromStdString

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4

            • Login

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