Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Objective-C types to QString
Forum Update on Monday, May 27th 2025

Objective-C types to QString

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 2 Posters 902 Views
  • 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.
  • S Offline
    S Offline
    SyntaX
    wrote on 27 Apr 2020, 10:49 last edited by
    #1

    I feel a little bit lost here :/

    What I am trying to do is just print values in out log output, for debug reasons:

    (void) someMethodWithNSParam:(NSInteger)param {
    
        //how to convert param to a QString?
        myLogger("myfunction", "received param: " + QString::fromStdString(std::to_string(param)));
    }
    

    I have to call myLogger, which takes QStrings as parameters.
    Is there an easy way to convert Objective-C types to QStrings?

    As seen, I managed to make a conversion to c string and than to QString, but I can't imagine, this is really necessary? xD
    I would like to avoid the creation of a variable, because its only a simple output and if there is a proper way to do this, this would be much more readable for a lot of params.

    Also, I know, that I just could print the values using qDebug, but I have to propagate all output to our custom log function.

    Is there a documentation of supported Objective-C type conversions in Qt or at least proper best practices available anywhere?

    best regards
    SyntaX

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Apr 2020, 18:02 last edited by
      #6

      You'll have two conversions anyway however using the Objective-C types all the way will likely be simpler.

      See this stack overflow answer. You can make it a one liner to get your QString.

      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
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 27 Apr 2020, 11:13 last edited by
        #2

        Hi,

        QString::fromNSString

        As a rule of thumb, look at the classes that have names resembling. Not all NSXXX types are going to be supported but the classics like NSString, NSURL have methods for that in Qt.

        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
        • S Offline
          S Offline
          SyntaX
          wrote on 27 Apr 2020, 15:35 last edited by
          #3

          Thanks @SGaist,
          the conversion between QString and NSString is straight forward, I think,
          but I was wondering if other non-primitive types have a proper conversion method offered by Qt?

          For me is seems like the double conversion (from NSXXX to NSString and then to QString) is necessary :/

          Maybe there is a way with work with raw bytes and use QString::FromRawData or FromLocal8Bit or something... if anyone know a more efficient solution than my approach, pls let me know :D

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 27 Apr 2020, 15:51 last edited by
            #4

            What primitives do you have in mind ? Qt won't cover all of macOS specific data types because it's out of scope.

            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
            0
            • S Offline
              S Offline
              SyntaX
              wrote on 28 Apr 2020, 15:12 last edited by
              #5

              In that special case I am looking for a proper NSInteger to QString conversion.
              As mentioned above, I can get around with -> to std string -> to QString,
              but this seems a bit of a overkill to me and I don't know any better alternatives :/

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 28 Apr 2020, 18:02 last edited by
                #6

                You'll have two conversions anyway however using the Objective-C types all the way will likely be simpler.

                See this stack overflow answer. You can make it a one liner to get your QString.

                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
                0
                • S Offline
                  S Offline
                  SyntaX
                  wrote on 29 Apr 2020, 11:38 last edited by
                  #7

                  @SGaist said in Objective-C types to QString:

                  stack overflow answer

                  Thank you @SGaist, I am posting the answer from SO here, for traceability reasons.

                  Modern Objective-C

                  An NSInteger has the method stringValue that can be used even with a literal

                  NSString *integerAsString1 = [@12 stringValue];
                  
                  NSInteger number = 13;
                  NSString *integerAsString2 = [@(number) stringValue];
                  

                  Very simple. Isn't it?

                  Swift

                  var integerAsString = String(integer)
                  
                  1 Reply Last reply
                  1

                  1/7

                  27 Apr 2020, 10:49

                  • Login

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