Hello,
It is possible to create Qt widgets in C++ and in the HDK. It's fairly straightforward and very similar to how you would create widgets in Python.
However, there are a few things to keep in mind:
1) Never try to create an instance of QApplication nor run the
exec()
method on QApplication. Houdini already does this on startup and doing the work again can produce unexpected behavior, such as a crash.
2) Avoid using smart pointers to manage Qt objects. Qt already semi-manages its own objects so using smart pointers will likely lead to crashing. Crashing can happen if the smart pointer deletes the referenced Qt object "too early" and Qt then tries to access the object, or at application shutdown when Qt cleans things up. If you need to delete a Qt object before shutdown, then use Qt's
.deleteLater()
method:
https://doc.qt.io/qt-5/qobject.html#deleteLater [doc.qt.io]
Or if you absolutely need to use smart pointers, then consider setting a custom deleter on the smart pointer that calls
.deleteLater()
instead of outright deleting the referenced object.
Anyway, I'm guessing that the use of
std::unique_ptr
in the sample code is leading to crashing in Houdini.
Also, if you need to access the singleton QApplication object, then either call
QApplication::instance()
or use the global
qApp
pointer. Both are available in the <QtWidgets/QApplication> header and both are mentioned in Qt's documentation:
https://doc.qt.io/qt-5/qapplication.html [doc.qt.io]
And I don't see any indication in the sample code that the widget is created in a separate thread. If that were the case, you would normally see a Qt warning or error message in the terminal about a Qt object created not in the main UI thread.
Cheers,
Rob