user1899020 user1899020 - 24 days ago 7
C++ Question

How to trigger an action without using signal-slot connection?

The usual way to trigger a Qt action is to use signal-slot connection. Any other way to do it since my function is not a slot? Such as some direct calls?

Answer

You can connect signals to non-slot methods using the new QObject::connect syntax in Qt 5. It looks like this:

connect(action, &QAction::triggered, this, &MyClass::doSomeFunction);

In this example, MyClass::doSomeFunction does not need to be a slot. Here is a more in depth explanation.

If you actually want to trigger a QAction, you can just do so directly, without using signals or slots:

action->trigger();