Max Frai
Max Frai

Reputation: 64366

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

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

Upvotes: 2

Views: 2052

Answers (3)

dev
dev

Reputation: 2200

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

Upvotes: 1

Edward Strange
Edward Strange

Reputation: 40897

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.

Upvotes: 2

tibur
tibur

Reputation: 11636

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

Upvotes: 1

Related Questions