// create a button QPushButton *button = new QPushButton("Click me!", this); // connect event handler to button click signal connect(button, SIGNAL(clicked()), this, SLOT(handleButtonClicked())); // event handler function void MainWindow::handleButtonClicked() { qDebug() << "Button clicked!"; }
// override keyPressEvent function to handle keyboard events void MainWindow::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Escape) { qDebug() << "Esc key pressed!"; } }
// override mousePressEvent function to handle mouse events void MainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { qDebug() << "Left mouse button clicked!"; } }In summary, EventHandler is a class in the Qt library that provides a way to handle events in event-driven programs. It is used to connect a GUI widget or program to an event, and execute code when the event occurs.