Hi guys,
Can anyone recommend what are the possibilities of creating direct Qt C++ bindings with Nim?
I would just like to know the principles of all the options (simple examples are of course very welcome) that are available. I would like to give it a go at creating something, but would like to have some directions, so that I'm not heading to a dead-end from the start.
Thanks
You can try to use https://github.com/PMunch/futhark
Otherwise you can take a look at https://github.com/SciNim/flambeau as an example of a C++ wrapped library.
Also you can look at https://github.com/Clonkk/nim-cppstl which wrap some C++ STL utilities
There’s https://github.com/trey-c/nim-qt
It’s focused on QML objects but uses a direct C++ wrapper. See for example https://github.com/trey-c/nim-qt/blob/master/qt/private/core/list.nim
I’d start from there. If you had the basic suite of Qt types that go a long ways. Though it’d be cool if furthark could auto gen the c++ wrappers better.
Looking at all the examples of C++ bindings that are mentioned above, the closest that comes to direct bindings is https://github.com/trey-c/nim-qt, but I think even the author realized that creating direct bindings is too much work compared to QML.
But I was also thinking ahead: even if the direct bindings could be made, I can see no clear way of using inheritance in Nim like it is used in C++ for creating new widgets.
So I am starting to think that a better approach would be to write something native in Nim: maybe Fidget (Fidgetty) or nimx?
Sorry to be late at the party but I think that it is possible to make Qt work with Nim. Just have a look at how a Go binding to Qt was created. https://golangexample.com/qt-binding-for-golang-aims-get-gos-compile-speed-again/
They just compiled Qt to a library and they created pseudo code to "mimic Qt calls". The only two problems with this wrapper is that the library looks to be "abandoned" and that Qt6 is not supported. That said, it looks to be complete.
So... if you want it to work with Nim, then I suggest to create a Go wrapper instead of copying the Go code to Nim. The good part is that Go is "easy", because it is simple.
I have written something that parses the Qt headers and then emits nim files, together with some macros that allow inheriting from a QObject.
It is using treesitter for parsing, but there are too many exceptions for it to work properly, so only a small portion of headers have been converted. So now I'm rewriting it to use cppast for the parsing, and generate the nim files. Hopefully that allows an easier path to convert all headers :)
Excellent 👍👍👍
Do you have a hello world that actually works? Will you release the library?
"But I was also thinking ahead: even if the direct bindings could be made, I can see no clear way of using inheritance in Nim like it is used in C++ for creating new widgets.
So I am starting to think that a better approach would be to write something native in Nim: maybe Fidget (Fidgetty) or nimx?"
I think that you are wrong about making new widgets. Who wants to make new widgets? That seems... wrong to me.
But the approach that they did with https://golangexample.com/qt-binding-for-golang-aims-get-gos-compile-speed-again/ I like a lot!
Why? Because compiling Qt is a ONE time operation. After that you can use the Qt stuff. You don't need to compile everything again and again, which I think is not the right approach.
So, I still think that creating a go2nim would be the better approach. They already created the entire QT wrapper, why try to reinvent the wheel again?
For the widgets it is: https://github.com/kitech/qt.go/tree/master/qtwidgets For the UIC it is: https://github.com/kitech/qt.go/blob/master/cmd/go-uic/main.go For the GUI it is: https://github.com/kitech/qt.go/tree/master/qtgui
And there is more, such as networking, qtquick etc.
To think about it, https://golangexample.com/qt-binding-for-golang-aims-get-gos-compile-speed-again/ probably is probably not the right answer. It has too much troubles involved and depends heavily on Go to deal with C++. I found a different "solution", that is a dlang binding https://github.com/tim-dlang/dqt But it is still "experimental". You can look at other wrappers at https://wiki.qt.io/Language_Bindings
I am curious about the code of jerous
Still working on it, but I don't have that much time and it's going slow. There's a lot of exceptions and corner cases going that have to be taken care of.
But so far I have most of QtCore already converted. Next up QtGui and QtWidgets, which should hopefully go smoother. Then I'll post here an update :)
Take your time but keep us informed ;-)
If you need more info about Qt then maybe I or someone else can help you.
I currently have a complex application in Qt for Python, which has a side effect of horrible first-time startup time (up to 30 seconds) on Windows. All subsequent runs are ok, under 3 seconds, but even that is quite a lot. So I tried a few nimqml examples and already noticed a slight first-time delay on these, so I'm worried that this will balloon upwards, just like in Qt for Python.
The second worry is styling: I have heavily customized overridden painting routines on QTreeWidgets , QTabWidgets and a few others, and I have no idea if that kind of heavy customization can be efficiently reproduced with QML alone.
Note for people reading this: that this has nothing to to with nimqml, it's a downside of QML.