Using Qt with Alternative Programming Languages

Using Qt with Alternative Programming Languages - Part 1

By Jeff Tranter

By Jeff Tranter 

Most Qt developers use C++ (the language that Qt is written in), often in association with QML. There are however, other programming languages that can be used with Qt. In this blog post, we'll look at some of the alternative language options.

Rationale

You may wonder why anyone would use a language other than C++, as it is the most well supported language for Qt programming. Some possible reasons include a personal preference based on one's knowledge or experience, or a particular programming language may be a better fit to the problem domain at hand.

Scripting languages such as Python, for example, can be a good choice for new programmers, as the core language is less complex than C++ (Python takes care of memory management and is not strongly typed, for example) and can be used to quickly prototype new code without having to go through a compile step.

Another reason can be the requirement to use legacy code that is written in another programming language. However, it is usually straightforward to call other languages from C++, typically using C linkage. ICS has worked on many customer projects that used C++ but included and linked with legacy code written in C, FORTRAN, Java, or other languages (sometimes multiple languages in the same application).

The disadvantages of using Qt with languages other than C++ can include restrictions on the versions of Qt that are supported, incomplete support for all of Qt's modules, and poor code quality if the bindings are not well tested or maintained.

Let's look at some of the available language bindings for Qt.

Ada

Ada is a structured, statically typed, object-oriented programming language, extended from Pascal and other languages. It was originally designed under contract to the United States Department of Defense (DoD) as a standardized programming language to supersede the hundreds of different programming languages then used by the DoD.

QtAda 4 is a set of Ada 2005 language bindings to the Qt libraries. It is offered under a number of different commercial licenses as well as the GNU GPL. It doesn't appear to be actively maintained. It supports Qt version 4.6, with the latest release listed as a snapshot from 2012.

BASIC

BASIC, an acronym for Beginner's All-purpose Symbolic Instruction Code, was originally developed at Dartmouth College as a programming language that could be used by students in fields other than science and mathematics.

Versions of BASIC became widespread on microcomputers in the mid-1970s and 1980s, and anyone who started programming during that era probably cut their teeth on some version of BASIC. It continues to exist in various forms including Microsoft's Visual Basic.

Basic for Qt 5, formerly known as KBASIC, is a full-featured object-oriented dialect of BASIC that uses Qt as its underlying cross-platform toolkit.

It runs on Linux, Windows, and Mac OS X and is a commercial product, based on Qt version 4.8.0. It is not clear if it is still well supported, as the most recent version is dated 2012.

C#

C# is a programming language, very similar to Java, developed by Microsoft as part of .NET. The Mono project is an open source C# compiler and language run-time implemented independently, although as Microsoft publishes portions of C# as open source software, the Mono project is also adopting some of it.

QtSharp 6 is a set of Mono/.NET libraries that wrap Qt, allowing it to be used from C#. It uses the CppSharp code generator tool to wrap the Qt C++ APIs.

The code is actively maintained, but the project appears to be in the early stages of development.

D

The D programming language is an object-oriented, imperative, multi-paradigm system programming language created by Walter Bright and Andrei Alexandrescu. Based on C++, it is considered a distinct language, and takes some features from other modern programming languages.

QtD 7 is a set of bindings for the Qt framework to the D programming language. It uses a code generator based on Qt Jambi, which is distributed under the GPL license, while the rest of QtD is distributed under the Boost Software License.

The current version supports the Qt classes related to GUI functionality. The last code check in was about four years ago, and while the source code is still available, is it no longer actively developed by any of the original developers.

Go

Go, also known as Golang, is a programming language developed by Robert Griesemer, Rob Pike and Ken Thompson at Google (the latter two were key contributors to the UNIX operating system, and its successor, Plan 9). It has a syntax derived from the C programming language, to which it adds a number of features including garbage collection and type safety.

There is an open source project 8, to create a Go package that adds support for Qt's QML language to the Go language. It runs on the Linux, Windows and Mac OS platforms. Currently it is considered to be at an alpha stage and is still in development.

Haskell

Haskell is a purely functional programming language. Every function in Haskell is a function in the mathematical sense. There are no statements or instructions, only expressions that cannot mutate variables or access state.

HsQML or Qt Quick for Haskell 9 is a Haskell binding to Qt Quick. HsQML allows you to bind together front-end designs written in QML with back-end logic written in Haskell to create complete applications using the strengths of both.

Supporting Qt 5, it runs on the Windows, Linux, and Mac OS platforms, is Open Source and available under a BSD license.

There was another set of Qt bindings of Qt Widgets to Haskell 10 but it has been unmaintained for about five years now.

Java

Java is a general-purpose programming language that compiles to a platform independent byte code that can run on any Java virtual machine. Originally developed at Sun Microsystems and later acquired by Oracle Corporation, much of the technology has been released under the GNU General Public License.

Qt Jambi 11 is a Java language binding for Qt. It enables Java developers to use Qt within the Java programming language. In addition, the Qt Jambi generator can be used to create Java bindings for other Qt libraries and future versions of Qt.

Qt Jambi supports Linux and other flavors of UNIX, as well as Mac OS X and Microsoft Windows. Jambi was a commercial add-on for Qt offered by Nokia, but the open source community now maintains the project as support from Nokia ended in 2010.

Currently it supports Qt version 4 and does not yet support Qt 5.

JavaScript

JavaScript, also known as ECMAScript, is a dynamic programming language commonly provided within web browsers.

There is no support for writing Qt applications entirely in JavaScript, but Qt supports JavaScript within the QML language, as well as in the Qt Script module.

Both Qt Script and QML contain independent JavaScript engines. In order to avoid this duplication, as of Qt 5.5.0, Qt Script has been deprecated and users are encouraged to use the QJSEngine class instead, which uses the same JavaScript engine as QML. At the moment, QJSEngine does not provide all of the capabilities of Qt Script, but the situation is expected to improve in future Qt releases.

Lisp

Lisp is a family of computer programming languages. The name derives from "List Processing" and linked lists are one of the Lisp language's major data structures. Lisp source code itself is made up of lists. Common Lisp (CL) is a dialect of the Lisp programming language that has been published as an ANSI standard.

CommonQt 12 is a Common Lisp binding to Qt. It works with a number of Common Lisp implementations including SBCL and Clozure. It supports Qt 4 (Qt 5 is not yet supported) on the Linux and Windows platforms.

Lua

Lua is a lightweight multi-paradigm programming language designed as a scripting language. It is cross-platform, written in ANSI C, and has a simple C API. Lua is intended to be embedded into other applications, and is the most popular scripting language for game programming.

The goal of the QtLua library 13 is to make Qt 4 and Qt 5 applications scriptable using the Lua language. It is an alternative to the QtScript module.

It provides useful C++ wrapper classes to make C++ and Lua objects accessible from both Lua and C++. It takes advantage of the Qt meta object system to expose QObject members to Lua scripts.

QtLua is not designed for developing an application in pure Lua, but rather to extend a Qt C++ application using Lua as scripting language.

Summary

In selecting a language binding for Qt, it is important to evaluate how complete, stable, and maintained the package is before committing resource to it. Many open source projects start off well, and then become abandoned or unmaintained. You may need to be willing to maintain it yourself unless you select a commercial solution. It is also important to check the licensing of the software, as it is often different from the license for Qt itself.

In researching this blog post, I was surprised to find no less than 20 Qt bindings for other languages. In order to cover them all, we'll continue the discussion in the second part of this blog series.

References

  1. Programming Language Support and Language Bindings, Qt Project wiki page, https://wiki.qt.io/Category:LanguageBindings
  2. List of language bindings for Qt 5, Wikipedia article, http://en.wikipedia.org/wiki/List_of_language_bindings_for_Qt_5
  3. List of language bindings for Qt 4, Wikipedia article, https://en.wikipedia.org/wiki/List_of_language_bindings_for_Qt_4
  4. QtAda, project web-site, http://www.qtada.com/
  5. Basic For Qt, product web-site, http://www.q7basic.org/
  6. QtSharp: Mono/.NET bindings for Qt, project web-site, https://gitlab.com/ddobrev/QtSharp
  7. QtD, Open Source Development for the D Programming Language, web-site, http://www.dsource.org/projects/qtd
  8. Graphical QML application support for the Go language, web-site, http://godoc.org/gopkg.in/qml.v1
  9. HsQML: Haskell binding to Qt Quick, web-site, http://www.gekkou.co.uk/software/hsqml/
  10. QtHaskell: A Set of Haskell Bindings for the Qt Widget Library, Arch Linux packages, https://aur.archlinux.org/packages/qthaskell/
  11. Qt Jambi - Qt for Java, project web-site, http://qtjambi.org/
  12. CommonQt: Common Lisp binding to the smoke library for Qt, web-site, https://common-lisp.net/project/commonqt/
  13. QtLua project, web-site, http://www.nongnu.org/libqtlua