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. Swift in Qt. Is there any manual or how-to?
QtWS25 Last Chance

Swift in Qt. Is there any manual or how-to?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 2.6k 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by bogong
    #1

    Hello all!

    I am trying to found any manuals and examples of implementing Swift in Qt. Is there any manual or how-to or examples? Could anyone provide link?

    I need somehow to call Swift methods in Qt/C++ class.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      http://www.perry.cz/clanky/swift.html
      might of interest.

      B 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        http://www.perry.cz/clanky/swift.html
        might of interest.

        B Offline
        B Offline
        bogong
        wrote on last edited by
        #3

        @mrjj said in Swift in Qt. Is there any manual or how-to?:

        http://www.perry.cz/clanky/swift.html

        Thx ... Is it compatible to *.pro file? Will it be built automatically or I need to add something into *.pro file?

        mrjjM 1 Reply Last reply
        0
        • B bogong

          @mrjj said in Swift in Qt. Is there any manual or how-to?:

          http://www.perry.cz/clanky/swift.html

          Thx ... Is it compatible to *.pro file? Will it be built automatically or I need to add something into *.pro file?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @bogong
          Hi
          Well, most of it is in .cpp / .h files.
          However, the bridge part seems to be in the Editor
          "Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. "
          so im not sure it can just work with only qmake. the Swift compiler must be involved too.
          Also, this might only be needed for swift calling c++ code.
          ( actually, it can only call c code it seems)

          B 1 Reply Last reply
          1
          • mrjjM mrjj

            @bogong
            Hi
            Well, most of it is in .cpp / .h files.
            However, the bridge part seems to be in the Editor
            "Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. "
            so im not sure it can just work with only qmake. the Swift compiler must be involved too.
            Also, this might only be needed for swift calling c++ code.
            ( actually, it can only call c code it seems)

            B Offline
            B Offline
            bogong
            wrote on last edited by
            #5

            @mrjj Thx will try it soon.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bogong
              wrote on last edited by bogong
              #6

              FINALLY!!! Almost 3 years been trying to find solution for using Swift in Qt. For now found the way.
              Got developed own solution for it based on:

              • https://stackoverflow.com/questions/18854750/qmake-extra-compiler-execution-order
              • https://forum.qt.io/topic/19308/solved-compiling-fortran-code-with-gfortran-within-a-c-project
              • https://github.com/ArboreusSystems/arboreus_examples/tree/master/objective-c/UsingSwift/UsingSwift_v1

              This example using QMake. For CMake haven't found proper way yet. For now have this kind of troubles https://developer.apple.com/forums/thread/722012 with project that uses CMake.

              How it works briefly:

              1. Define QMAKE_EXTRA_COMPILERS parameters in *.pro file
              2. Create bridge header between Swift an Objective-C SWIFT_OBJC_BRIDGING_HEADER and define SWIFT_OBJC_INTERFACE_HEADER_NAME (this interface header will be created automatically by XCode at time of compilation)
              3. Create wrapper for Objective-C and C++ via Objective-C++ class (*.mm file) and include SWIFT_OBJC_INTERFACE_HEADER_NAME
              4. Translate Swift data and objects to C++ in Objective-C++ class. This point might be very tricky because you need to create object in C++ that will have the same data that has Swift class. All of it depends on application logic. Try not use complex data structures. Simplify everything that possible when translating it from Swift to Qt, it will make your life happier.

              For now issue closed. Trying to find the way to use Swift with CMake in Qt projects. Happy coding. Here the video of how it works in iOS.

              mrjjM 1 Reply Last reply
              3
              • B bogong

                FINALLY!!! Almost 3 years been trying to find solution for using Swift in Qt. For now found the way.
                Got developed own solution for it based on:

                • https://stackoverflow.com/questions/18854750/qmake-extra-compiler-execution-order
                • https://forum.qt.io/topic/19308/solved-compiling-fortran-code-with-gfortran-within-a-c-project
                • https://github.com/ArboreusSystems/arboreus_examples/tree/master/objective-c/UsingSwift/UsingSwift_v1

                This example using QMake. For CMake haven't found proper way yet. For now have this kind of troubles https://developer.apple.com/forums/thread/722012 with project that uses CMake.

                How it works briefly:

                1. Define QMAKE_EXTRA_COMPILERS parameters in *.pro file
                2. Create bridge header between Swift an Objective-C SWIFT_OBJC_BRIDGING_HEADER and define SWIFT_OBJC_INTERFACE_HEADER_NAME (this interface header will be created automatically by XCode at time of compilation)
                3. Create wrapper for Objective-C and C++ via Objective-C++ class (*.mm file) and include SWIFT_OBJC_INTERFACE_HEADER_NAME
                4. Translate Swift data and objects to C++ in Objective-C++ class. This point might be very tricky because you need to create object in C++ that will have the same data that has Swift class. All of it depends on application logic. Try not use complex data structures. Simplify everything that possible when translating it from Swift to Qt, it will make your life happier.

                For now issue closed. Trying to find the way to use Swift with CMake in Qt projects. Happy coding. Here the video of how it works in iOS.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @bogong
                Well fought and congrats.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  bogong
                  wrote on last edited by
                  #8

                  The solution for using Swift found. Here the published example when you using CMake subdirectories and here

                  The problem when you using it's like subtarget not solved yet. The Swift code pieces must be in main target. If it's not, you will have this kind of troubles.

                  If anyone wants to solve troubles with implementing Swift in subtargets you can play with this project

                  For me all is working pretty good like it's described above.
                  For me issue totally closed.
                  Happy coding!

                  1 Reply Last reply
                  1

                  • Login

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