To do a Rust GUI

2018-06-09

Rust Qt Binding Generator (Logo by Alessandro Longo)

Rust Qt Binding Generator lets you combine Rust code with a Qt1 graphical application. A previous blog shows how to make a simple clock. It’s a good idea to read that post before reading this more advanced post, because in this post we are getting serious.

This blog post shows how to write a to-do application. The data model is a list of to-do items. The source code for this example is available in the folder examples/todos in the Rust Qt Binding Generator repository.

Here is a screenshot of the finished application. The to-do application shows the steps to implement the to-do application. This application was the subject of a presentation on Rust Qt Binding Generator.

The to-do application

Model/View programming

Model/View programming is a big thing in Qt. It’s everywhere. It’s not trivial, but powerful. Please bear with me for a bit as we’ll talk about how this works. Rust Qt Binding Generator does most of the hard work for you, but it does not hurt to know a bit of how it does this.

One C++ class is the basis for all the model/view programming in Qt: QAbstractItemModel. As the name says, QAbstractItemModel is an abstract model for items. If you want to have a list, a tree or a table in your program, you’ll be deriving a class from QAbstractItemModel.

When your data is in a class derived from QAbstractItemModel, you can put that model in one or more simultaneous widgets. In Qt, you put a list in QListView, a tree in a QTreeView, and a table in a QTableView. QComboBox and QComplete also use a model for their data.

In QML, models are even more important. QML has a ListView, TreeView, and TableView as well, but also GridView, Repeater, MapItemView and more.

Any non-trivial application will have Qt models. So we’ll show you how to make a list model with Rust Qt Binding Generator.

A to-do application

The to-do application in this post implements the specification of the TodoMVC website. ‘MVC’ stands for Model-View-Controller. In this post, Rust supplies the Model. The View and Controller are written in QML. The to-do list above contains seven items. The three items that start with ‘check’ are there for the curious that would like to see what code for the communication between Rust and C++ looks like.

The first step in the to-do is ‘Write bindings.json’. bindings.json is the file where you describe the model of your application. The data in the to-do application is simple. It is a list of to-do items. So we define an object of type List. Each list item has two properties: a boolean completed and a QString description. You can see these fields in the JSON snippet below.

Both fields, completed and description are writable so the to-do can be toggled and the description text can be changed.

Bindings.h, Bindings.cpp, and interface.rs.

Our list after running rust_qt_binding_generator

The Rust side

The generated file interface.rs contains a Rust trait that is derived from the data model. Here is the relevant part of the Rust trait:

one QML file. This file is about 200 lines. Below are some cut down snippets from that file.

At the top, the Rust data model should be imported.

notquick, a viewer for mail boxes that uses Rust Qt Binding Generator. For more examples with trees and threading, check out the folder demo.

1: pronounced as kjuːt

Comments

Post a comment