Connect without object
I want to connect some function as slot without class, can I do this:
void update() { }
int main()
{
QTimer timer = ...;
QObject::connect(timer, SIGNAL(timeout()), SLOT(u开发者_开发问答pdate()));
return 0;
}
The compiler says, that without object it's impossible.
AFAIK, you can only connect signals to slots, and slots can only exist as member functions of a Q_OBJECT.
While many people focus on the template vs. moc difference between Qt signals and boost::signals or GTKmm signals, THIS is the difference I ultimately care more about. Qt's signals are not as expressive and cause more dependencies than I want.
I still use Qt, but that's just because GTKmm accessibility is completely missing on win32 systems.
What you can do, of course, is make a subclass of QTimer that connects to its own timeout signal with a slot that generates a boost::signal that you CAN connect to your external function. Take care of the issues in using boost signals in Qt though, I just use signals2 to avoid it entirely AND I get thread safety.
You need that Qt recognize the slot. To do so, you have to moc a class. So I would say impossible.
You can use Boost's signal slot mechanism. Boost Signal Slot
And if you are using Qt 4.1 or greater both can be used together as explained here Boost signals & slots with Qt
精彩评论