Thoughts on Qt Bridge for Rust? (GUI)

I'm considering building a GUI application in Rust, however I'm not familiar with the current GUI landscape. The areweguiyet.com site led me to discovering the GUI framework Qt.

Apparently Qt recently released a crate that bridges Rust to their framework: qtbridge. It uses QML, their own markup language, to declaratively design the UI.

They uploaded a getting started video yesterday which I thought was a good watch. It lists out that you need the qtbridge crate, the Qt Framework (6.10 or higher for your specific OS), and a C++ compiler for their dependencies.

I'm curious if anyone else has thoughts or interests in using Qt for Rust? My alternative choice is egui. From my understanding egui is an immediate mode GUI, and Qt is retained.

My application doesn't need to be concerned between these two differences, but I like the idea of a retained GUI for performance reasons (IIRC immediate renders constantly which can? lead to overhead). Also, coming from C# WinForms I have a special place for callback functions/ event handlers.

Thank you!

If you haven't yet, take a look at gtk-rs. GTK is extremely opinionated nowadays, especially if you you use Adwaita, but is a very mature toolkit. You get a nice UI description language (blueprint), a GUI designer (Cambalanche) and even a tool for fast prototyping (Workbench).

yep, that's correct.

the term "immediate mode" describes the API style, not how it's implemented. this article and the followups are good reads for understanding the challenges of gui architecture in rust.

in general, immediate mode is more natural for use cases where the ui is constantly being updated/refreshed, but it does not necessarily require redrawing the ui continously every frame.

in fact, if you are using the eframe crate, by default the app runs in "reactive" mode, i.e. it only update the ui when triggered by external events (and scheduled redraws for animation purpose). if you want to update the ui continuously, you'll need to call ctx.request_repaint() yourself every frame.

if you are writing your own egui integration instead of using eframe, then you'll need to handle the event loop and repaint schedule yourself.

there's nothing wrong inherently with callback based event handling, for example, xilem uses them. the problem is scope and states.

gui frameworks in other languages don't have the concept of borrows, so they are not designed with proper state management in mind. when a callback needs to access states from a "wider" scope, it is inevitable the states are captured and shared by many callbacks, which is a major pain point when porting traditional gui architectures to rust. you may want to read this article.

very often, you end up with interior mutability wrappers everywhere, e.g. Rc<RefCell<...>>, and usually you need Weak<RefCell<...> too, to avoid reference cycles.

that's why I personally prefer immediate mode kits such as egui when I do gui code, because it fits naturally with rust's model of ownership and borrows, since you don't need to "capture" the app states just to handle events.


so conclusion? unless you need interop with other languages like C++, or you need Qt specific features like QML scripts, or you are already experienced and fluent in Qt, I would not recommend using Qt in rust. egui is my personal favorite and top recommendation.

that said, I have not checked the qtbridge crate you mentioned. if they solved the problem with closures and borrow checker, my previous assumptions could be invalid. in that case, I would love to give it a try and see how they solved it.

I want to explain this point a little bit more.

the "impedance mismatch" of using rust to do callbacks for "traditional" retained mode framework is NOT fundamental to callbacks or retained mode, but the architectural design of the framework.

for example, if a callback is only allowed to access states that are local to the widget, or in other words, it is captureless, or at least it doesn't capture shared states, then the borrow checker is totally ok.

on the other hand, a callback is useless if it were restricted to the widget and couldn't access the app states: you have to be able to access the app states in order to write the app logic.

traditionally, an even callback receives widget specific and event specific parameters. but the app states are stored alongside the callback function as some opaque user data/context pointer, typically attached to the widget and retrievable from widget handle, e.g. GetWindowLongPtr on windows, or the this pointer in many OOP wrappers. in rust, this user context is typically the captured environment of a closure.

the problem is, this user data/context is owned by the application, and must be wired up when the callback function is registered (i.e. must be captured when the closure is created). this forces all the callbacks to have shared ownership of the app states, i.e. Rc<RefCell<...>> in rust.

but it doesn't have to be like this! to designed a principled retained mode gui framework for rust, we can let the framework to manage the life cycle of the app states, e.g. moving the ownership into the event loop.

because the callbacks are dispatched from the event loop, which is single threaded, we can pass this state as an argument, as an exclusive reference, to the callbacks. this eliminates all the ergonomic issues related to interior mutability and weak pointers.

however, this approach still has the problems of composibility: all event handlers use the same type for the app states, which means your app must have a monolithic design, you can't break it into components, not easily at least.

to solve the composibility, we need an adapter layer that can give a "focused view" of a larger state value. if you have used xilem, you must have seen the lens function already.

the final piece of the puzzle is reactivity: when the app states get updated, how to notify the framework to redraw the ui? well, there's no definitely "best" solution to this problem. options may include (non exhaustive):

  • conservatively redraw the ui after every (batch of) event callbacks

  • only redraw when the user explicitly notified the framework, similar to the egui ctx.request_repaint() api

  • the callback must return an (optional) action, and the framework will take the action accordingly

  • the callbacks receives a copy of the states, the framework diff the states before and after callbacks, and redraw the ui when the states are changed.

  • require the app state to implement some trait that can tell the framework whether it has changed and needs to redraw the ui.

  • ditch the idea of arbitrary user defined app state type, for the app state to use some reactive signal wiring mechanism instead.