@Perdrix said in Problem with inheritance and QObject/QDialog:
A very short example of what you have in mind would be most helpful
I assume that this is in response to callback functions.
Basically, you would have two functions: one for registering a callback with its associated data and the other to call the callback. For simplicity I'll assume there will only be one single callback at a time and I'll use global data. It is easy to make this a little nicer.
void (*progressCallback)(int progress, void *data) = nullptr;
void *progressCallbackData = nullptr;
void registerProgressCallback(void (*callback)(int progress, void *data), void *data)
{
progressCallback = callback;
progressCallbackData = data;
}
void updateProgress(int progress)
{
if(progressCallback)
{
progressCallback(progress, progressCallbackData);
}
}
class MyProgressClass
{
public:
MyProgressClass()
{
registerProgressCallback(std::mem_fun(&MyProgressClass::updateProgress), static_cast<void*>(this));
}
void updateProgress(int progress)
{
//...
}
};
This would be the rough outline for using callbacks. I am not entirely sure about std::mem_fun (or is it std::mem_fn now?). Maybe you need an additional wrapper function to cast the void pointer back to a pointer to MyProgressClass. Maybe lambdas would work as well. Things look a little easier if you don't use a function pointer but std::function<void(int,void*)> instead. I am also not entirely sure if I got the function pointer syntax right.
registerProgressCallback() and updateProgress() could also be static functions of a class together with class variables storing all callbacks and their associated data in vectors.
The advantage of the void pointer for additional data is that you can just declare a struct and put all function parameters into this. Then write a wrapper function that will unwrap the data from the struct and call you progress function with an arbitrary amount of (fixed) parameters.