How to define an OnClick event handler for a button from within Qt Creator?

In the designer,

  1. add Push Button to the form
  2. right click the Push Button
  3. select "Go to slot..."
  4. select "clicked()" signal
  5. done

The terms are different from .NET, so in this case we are talking about signals and slots, and the signal emitted when QPushButton is clicked is called clicked() instead of OnClick.

Reading the Qt's documentation about signals and slots is recommended.


In header file:

private slots:
    void exit_app();

in xyz.cpp:

connect(ui.button_name, SIGNAL(clicked()), this, SLOT(exit_app()));

define the exit_app() func that SLOT calls.

void QtTest2::exit_app()
{
    QApplication::exit();
}