PyQt - Action Event



When using PyQt an action event occurs when a user engages with a graphical user interface component linked to a function like clicking a button choosing a menu option or using a keyboard shortcut. Managing these events includes linking these interactions to the functions or procedures that carry out the intended actions.

Key Components of Action Events in PyQt

  • QAction − A QAction signifies an activity that the user can initiate. It may be linked to visual interface components, like menu options, toolbar icons and keyboard shortcuts.
  • Signal-Slot Mechanism − PyQt uses a signal-slot mechanism to handle events. Signals are emitted when a particular event occurs, and slots are functions or methods that are called in response to those events.
  • Event Handlers − PyQt provides predefined event handlers for common user interactions, such as triggered() for when the action is triggered by the user.

Methods for Handling Action Events

Method Description
triggered() Called when the action is triggered by the user.
hovered() Called when the mouse cursor hovers over the action.
toggled() Called when the action's state changes, typically for toggleable actions like checkboxes.

Example 1: Simple Button Click Event

In the below example, we'll create a PyQt application with a single button. Clicking the button will trigger an action event that displays a message.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox

class MainWindow(QMainWindow):
   def __init__(self):
      super().__init__()

      self.setWindowTitle("Button Click Example")

      button = QPushButton("Click Me!", self)
      button.clicked.connect(self.display_message)

   def display_message(self):
      QMessageBox.information(self, "Message", "Button Clicked!")

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())

Output

pyqt action event example 1

Example 2: Menu Item Selection Event

In the below example, we'll create a PyQt application with a menu bar. Selecting a menu item will trigger an action event that displays a message.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox
from PyQt6.QtGui import QAction

class MainWindow(QMainWindow):
   def __init__(self):
      super().__init__()

      self.setWindowTitle("Menu Selection Example")

      action = QAction("Show Message", self)
      action.triggered.connect(self.display_message)

      menu = self.menuBar()
      file_menu = menu.addMenu("File")
      file_menu.addAction(action)

   def display_message(self):
      QMessageBox.information(self, "Message", "Menu Item Selected!")

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())

Output

pyqt action event example 2

Example 3: Toggleable Action Event

In the below example, we'll create a PyQt application with a toggleable action. Clicking the action will toggle its state and display a message accordingly.

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox, QPushButton
from PyQt6.QtGui import QAction

class MainWindow(QMainWindow):
   def __init__(self):
      super().__init__()

      self.setWindowTitle("Toggleable Action Example")

      self.toggle_action = QAction("Toggle", self, checkable=True)
      self.toggle_action.triggered.connect(self.display_message)

      self.addAction(self.toggle_action)

      self.toggle_button = QPushButton("Toggle Action", self)
      self.toggle_button.clicked.connect(self.toggle_action.trigger)

   def display_message(self, checked):
      if checked:
         QMessageBox.information(self, "Message", "Action Checked!")
      else:
         QMessageBox.information(self, "Message", "Action Unchecked!")

if __name__ == "__main__":
   app = QApplication(sys.argv)
   window = MainWindow()
   window.show()
   sys.exit(app.exec())

Output

pyqt action event example 3
Advertisements