Generating Qt Q_OBJECT classes pragmatically
How might one go about making a Q_OBJECT based class (one that has signals and/or slots) through metaprogramming? I don't care if it's templates or preprocessor, neither appears to work and I really need it too.
What I want is to be able to bind arbitrary function like things to Qt signals. Qt signals are incapable of this, boost signals are. So, I need a Qt object that can connect to a Qt signal and simply forward it on to a boost signal. I'll need to do this a bunch, so it would be nice to automate.
Th开发者_Go百科e main problem appears to be--no big surprise--the moc processor. It doesn't understand basic preprocessing (except ifs) and apparently can't do template classes either.
Any ideas?
Here's what I've discovered about the Qt moc system:
Using some of http://www.codeproject.com/KB/cross-platform/qt_reversing.aspx ...
Since I know that the generated object is going to have one, single slot, and nothing else (that's what it's for) generating the moc stuff with the preprocessor should be possible, though maybe difficult.
Most of the information in the qt_moc_data_???[] array can be hard coded. The only line of interest seems to be the slots and there'll be only one. The rest can be { 5, 0,0,0,1,14,0,0,0,0,0,0,0,0 ... slot line ...., 0}
The slot line is described in the above link.
The qt_moc_stringdata_??? needs to be something like: #object_name "\0\0" (param > 1 ? param-1:0 count commas) "\0" #functionname #paramtypelist
I believe all of that can be generated with the preprocessor. The tricky part is going to be that the slot line has to contain numbers that provide indexes to the qt_moc_stringdata_???
There's also some functions that need to be generated, and a variable of type QMetaClass or something... Most of that stuff should be fairly straight forward with the exception of the invoker, which has to reinterpret cast all the parameters.
Still though, I believe the preprocessor can generate all of it and that appears to be the only way to do this. The only help I got from the qtcenter forum was to use:
#ifdef MYCLASS
struct myclass : QObject
{
... a QObject definition...
};
#endif
... and that's just plain entirely insufficient to the task. Sometimes the answers people give make me wonder.
Unfortunately, of course, all this magic is going to depend a great deal on the version of the moc used.
I've found a better, easier way to make this happen. I've started a blog article series on creating this monster. See http://crazyeddiecpp.blogspot.com/2011/01/quest-for-sane-signals-in-qt-step-1.html
精彩评论