zs3.me

SwiftQt: Using the Qt GUI on Linux

Revision 11
© 2023-2024 by Zack Smith. All rights reserved.

Overview

This is a project to get Swift working with the Qt 6 GUI library to allow construction of conventional (imperative) GUIs.

The task of creating an open-source, declarative GUI builder, like QML (introduced in 2008) or SwiftUI (introduced in 2019), also seems feasible.

SwiftQt, as I'm calling it, has the purpose of helping me create personal apps on Linux with Qt and using Swift instead of C++ or Python, which could be beneficial in some ways.

Who knows, perhaps someday SwiftQt will also allow creation of Swift GUI apps on other platforms that Qt supports, like Android and Windows.

Is getting Swift to work with Qt easy? It consists largely of wiring things up so that Swift talks to C, which then talks to C++, and then in the opposite direction, for each of the classes and methods that would be useful. Equivalent objects must exist on both the C++ and Swift side, so some book-keeping is required.

Currently SwiftQt works with Qt 6 and Swift 5.9.2.

Why Qt?

Qt has been fairly reliable in my use of it. For basic GUIs it is fine.

However it is by no means perfect. There are certainly bugs in:

  • Qt 5's text editor widget.
  • Qt 6's video player widget.

Screenshots

A GUI built using manual layout i.e. setFrame.

 342

A GUI built using box layouts (horizontal and vertical).

 342

A GUI built using a grid layout.

 342

Q & A

Can SwiftQt be put in a Swift package?
Not at present. Mixed computer-language source directories are not currently allowed in packages on Linux.

Can SwiftQt be put in a shared library?
Seemingly not, but that's not 100% certain. I encountered problems trying to link with my swiftqt.a, specifically the linker said it was missing symbols of functions that were present in the object files. I'll work on that a bit later.

Is an open-souce SwiftUI possible?
Perhaps someday, if you want to write it. The foundation for such an effort would be a project like SwiftQt. However in keeping with Qt tradition it would likely need to implement QML, which preceded SwiftUI by a decade.

Will you implement every single Qt class and method?
No, as that would be a full-time, months-long effort. I don't need all of the classes myself and I'm not employed by Qt.

What classes will you add support for?
I'll mostly add those which I require for my personal projects.

Why isn't this on Github?
It is not necessary for everything to be on Microsoft-owned Github. Years ago, people said everything had to be on a site called Sourceforge, yet I discovered one day when I examined the legal fine print, that SourceForge's terms of service claimed that uploading resulted in a transfer of ownership to them.

Can others contribute?
Yes, you can send me source files. Make sure they are covered by the GNU Public License.

Download

This source code should be considered alpha.

Swift Compiler

The Swift compiler has been buildable and running on 64-bit Linux and Windows for a few years.

Swift for aarch64 Linux

Currently Debian for arm64 doesn't offer the Swift compiler, so here it is.

  1. USE IT AT YOUR OWN RISK.
  2. No warranty is offered or implied.
  3. It's provided AS-IS.

How to compile the compiler and its libraries yourself

 #!/bin/bash
 mkdir swift-source
 cd swift-source
 git clone https://github.com/apple/swift.git
 pushd swift
 git checkout swift-5.9.2-RELEASE
 popd
 ./swift/utils/update-checkout --clone \
   --tag swift-5.9.2-RELEASE
 time nice swift/utils/build-script -j2 \
   --preset=buildbot_linux,no_test \
   install_destdir=~/swift-install \
   installable_package=~/swift.tgz

2138155738