user1899020
user1899020

Reputation: 13575

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?

Upvotes: 2

Views: 6633

Answers (3)

sonichy
sonichy

Reputation: 1478

If connect in main(), it can be like this:

void windowAbout(){
   QMessageBox aboutMB(QMessageBox::NoIcon, "About", "This is a weather forcast !");
   aboutMB.setIconPixmap(QPixmap(":/icon.ico"));
   aboutMB.exec();
}
int main(){
   ......
   QObject::connect(aboutAction, &QAction::triggered, &app, &windowAbout);
   ......
}

Upvotes: 1

Anthony
Anthony

Reputation: 8788

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();

Upvotes: 14

Nemanja Boric
Nemanja Boric

Reputation: 22187

You can add slot which you will connect with the signal, and you can call your function within it.

Upvotes: 0

Related Questions