2

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(update()));
  return 0;
}

The compiler says, that without object it's impossible.

Max Frai
  • 61,946
  • 78
  • 197
  • 306

3 Answers3

2

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.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • 1
    It is perhaps worth noting that this functionality is being introduced in Qt 5. http://developer.qt.nokia.com/wiki/New_Signal_Slot_Syntax – leinir Feb 27 '12 at 07:04
1

You need that Qt recognize the slot. To do so, you have to moc a class. So I would say impossible.

tibur
  • 11,531
  • 2
  • 37
  • 39
1

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

dev
  • 2,180
  • 2
  • 30
  • 46