Calling multiple slots using a signal
-
Hi all,
Is there any better way to connect a signal to more than a slot than the one below, please?
connect(sender1, SIGNAL(triggered()), this, SLOT(foo_1())); connect(sender1, SIGNAL(triggered()), this, SLOT(foo_2())); connect(sender1, SIGNAL(triggered()), this, SLOT(foo_3()));
-
Hi! Better in what regard?
-
@Wieland
Hi! Better in what regard?
Good question! I meant the way professionals use.
-
Well, you could save pointers to the slots in a vector and then do stuff like this:
using foo_p = void (MainWindow::*)(); QVector<foo_p> fps; fps << &MainWindow::foo_1; fps << &MainWindow::foo_2; // ... fps << &MainWindow::foo_100; for (auto f: fps) connect(this, &MainWindow::triggered, this, f);
-
@Wieland
Not been taught these as they are rather advanced. For the time being I use the first version.
Thanks. -
@tomy
Hi
What wieland shows is just normal c++.
It is not considered advanced as such.
But yes, you could say for a c++ beginner, function pointers might be advanced.
He just put pointers to member functions in a list and
loop over them to connect them all.
If you had 200, it would be a much better design that a load of
connect statements.Anyway, its not really common to connect same signal to different slot in same class. You might as well fall foo_2 and foo_3 from foo_1.
But if the foo_x each lives in own classes, then its a pretty normal design.