How to use void return types in HDK (C++)?

   807   0   1
User Avatar
Member
4 posts
Joined: Nov. 2020
Offline
If I want to create an icon using the python Qt module, we have the functions:
import hou

createIcon(name, width=32, height=32)  QIcon
// or the newer version:
__init__(icon_name, width=None, height=None)

// Example usage:
myIcon = hou.qt.createIcon("SOP_polybevel",20,20) // <-- I can do whatever I want with myIcon
(see documentation here. [www.sidefx.com])
Both of these functions return the icon created from the specified Houdini icon name.

If I want to do the same thing in C++ with HDK I have the function:
#include <HOM_qt.h>

virtual void * 	_createIcon (const char *icon_name, int width, int height)=0
(see documentation here. [www.sidefx.com])
However, this function returns a void pointer, i.e. nothing (one could say the function doesn't return anything at all), and I don't know how to get the icon from the function so that I can use it like in the python example above.

There are tons of functions in the HDK that returns a void pointer whose python equivalence actually returns a value, which leads me to believe that I'm missing something incredibly simple. I'm suspecting a form of casting has to take place, but how can you cast a void to a concrete type? Could somebody point me in the right direction?

Apologies for asking a question that must be obvious to many of you who are familiar with the HDK.
Thanks in advance!

EDIT: I got it working. It turns out you can cast a void pointer to a concrete type. Here's some sample code (I've omitted some boiler plate code for brevity):

QWidget* myWidget = new QWidget(nullptr, Qt::WindowFlags::enum_type::Window);	// Create a new widget as a window.
myWidget->setGeometry(300, 300, 100, 100);

void* newIcon = HOM().qt()._createIcon("SOP_polybridge", 20, 20);	// We get a void pointer to the icon.
QIcon* convertedIcon = static_cast<QIcon*>(newIcon);			// Recast the void pointer to QIcon, this is what I missed.
QIcon icon = QIcon(*convertedIcon);
myWidget->setWindowIcon(icon);
myWidget->show();

I'm sure one could/should use HOMdel instead of static_cast in production code, though I haven't really looked into it.
Edited by testarut - Sept. 27, 2021 17:35:34
  • Quick Links