GithubHelp home page GithubHelp logo

plumpmath / qt-and-swift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from msorvig/qt-and-swift

0.0 0.0 0.0 30 KB

Qt and Swift interop research

Swift 5.12% Objective-C 3.38% QMake 1.93% C++ 89.57%

qt-and-swift's Introduction

Qt and Swift

This project contains testing code for ongoing R&D on using Qt in swift-based projects.

C++ and Swift

C++ and Swift interop is possible, but not at the level of Objective C++, where for example Objective C objects can be used from C++ code.

What we can do instead is export a C function from our C++ implementation, and then call it from Swift:

#define EXPORT __attribute__((visibility("default")))
extern "C" EXPORT NSView *createQtApplicationView();

let qtView = createQtApplicationView()
contentView.addSubview(qtView)

The function we are exporting creates an application view implemented using Qt. On the Qt side this a QWindow with for example Qt Widgets or Qt Quick content.

qmake and Xcode

Qt has special build and deployment requirements (moc, rcc, plugin deployment). Swift also has a bit of build logic. We'll let Qt build and deploy the part of the application that uses Qt, and then integrate that into a Swift- based Xcode project.

The common interface will be a framework: Qt produces one in compliance with the expected framework layout. Xcode can then build, deploy, and code sign it.

Building this sample code

  1. Build and deploy QtApplication.framework

    /path/to/qtbase/bin/qmake && make && /path/to/qtbase/bin/macdeployqt QtApplication.framework -deploy-framework

[deploy-framework is an undocumented research option available on newer versions of macdeployqt]

  1. Jump through a hoop and set the correct LC_RPATH on the cocoa platform plugin

    install_name_tool -add_rpath @loader_path/../../Frameworks/ QtApplication.framework/Contents/PlugIns/platforms/libqcocoa.dylib

[this step will eventually go away]

  1. Open and Build the QtAndSwift project

    open QtAndSwift.xcodeproj

Implementing createQtApplicationView()

See sample code. Note qt.conf which sets up plugin and import paths.

Setting Up the Xcode Project

If you want to recreate the Xcode project, or modify your own, here's how:

  1. Create a standard Cocoa/Swift project

  2. Add QtApplication.framework

    Build Phases -> Link Binary With Libraries: Add QtApplication.framework Build Phases -> "+" -> New Copy Files Phase: Add QtApplication.framework Build Settings -> Search Paths -> Framework Search Paths: Add ../QtApplication

  3. Create the Swift -> C bridging header

Create and add QtAndSwift-Bridging-Header.h to your project. Sample contents:

#import <AppKit/AppKit.h>
NSView *createQtApplicationView();
void destroyQtApplicationView(NSView *);

And add to build:

Build Setting -> Swift Compiler - Code Generation -> Objective C Bridging Header: QtAndSwift/QtAndSwift-Bridging-Header.h

qt-and-swift's People

Contributors

msorvig avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.