Trying to delete QComboBox items while hovering over them and pressing the "delete" key.
-
The code detects the current index of the item that is being hovered over correctly, but it seems that the KeyPress event isn't being detected while the focus is on the combobox drop down menu:
# Add the comboboxes to an array self.comboboxes = [ self.installed_apps_combobox1, self.installed_apps_combobox2, self.installed_apps_combobox3, self.installed_apps_combobox4 ] # Track the hovered item index self.hovered_index = -1 # Install event filters for each combobox popup to track hovering for combobox in self.comboboxes: combobox.view().viewport().installEventFilter(self) def eventFilter(self, source, event): # Track currently hovered over item if source in [combobox.view().viewport() for combobox in self.comboboxes]: if event.type() == QEvent.Type.MouseMove: hovered_pos = event.position().toPoint() for combobox in self.comboboxes: if source == combobox.view().viewport(): index = combobox.view().indexAt(hovered_pos).row() self.hovered_index = index print(f"Hovered over combobox at index: {self.hovered_index}") break # Reset hovered index when the mouse leaves the combobox view elif event.type() == QEvent.Type.Leave: print(f"Mouse left combobox view. Resetting hovered index from {self.hovered_index} to -1.") self.hovered_index = -1 # Capture delete key event when the combobox dropdown is open for combobox in self.comboboxes: if source == combobox.view().viewport(): # Check if the source is the combobox's view if event.type() == QEvent.Type.KeyPress and event.key() == Qt.Key.Key_Delete: print(f"Delete key pressed in combobox view. Current hovered index: {self.hovered_index}") # Debug print if self.hovered_index >= 0: combobox.removeItem(self.hovered_index) print(f"Removed item at index {self.hovered_index} from combobox.") self.hovered_index = -1 # Reset the hovered index return True # Event handled # Call the base class eventFilter to handle any other events return super().eventFilter(source, event)
-
Hi,
Which version of PySide/PyQt are you using ?
On which OS ?
Can you make your example complete so it's runnable ? -
Hi,
Which version of PySide/PyQt are you using ?
On which OS ?
Can you make your example complete so it's runnable ?@SGaist Here's the isolated code that anyone can run, it successfully detects the correct index when hovering over in the drop down menu, but I can't get it to work for detecting the key press.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QComboBox, QHBoxLayout from PyQt6.QtCore import Qt, QEvent class ComboBoxApp(QWidget): def __init__(self): super().__init__() self.setGeometry(100, 100, 500, 300) layout = QHBoxLayout() combobox1 = QComboBox() combobox2 = QComboBox() combobox3 = QComboBox() combobox4 = QComboBox() self.comboboxes = [combobox1, combobox2, combobox3, combobox4] for combobox in self.comboboxes: # Add each combo box to the layout layout.addWidget(combobox) # Install filter combobox.view().viewport().installEventFilter(self) # Fill with items for i in range(1, 11): combobox.addItem(f"Item {i}") # Set the layout self.setLayout(layout) def eventFilter(self, source, event): # Track currently hovered over item if source in [combobox.view().viewport() for combobox in self.comboboxes]: if event.type() == QEvent.Type.MouseMove: hovered_pos = event.position().toPoint() for combobox in self.comboboxes: if source == combobox.view().viewport(): index = combobox.view().indexAt(hovered_pos).row() self.hovered_index = index print(f"Hovered over combobox item at index: {self.hovered_index}") break # Reset hovered index when the mouse leaves the combobox view elif event.type() == QEvent.Type.Leave: print(f"Mouse left combobox view. Resetting hovered index from {self.hovered_index} to -1.") self.hovered_index = -1 # Capture delete key event when the combobox dropdown is open for combobox in self.comboboxes: # Check if the source is the combobox viewport if source == combobox.view().viewport(): if event.type() == QEvent.Type.KeyPress and event.key() == Qt.Key.Key_Delete: print( f"Delete key pressed in combobox view. Current hovered index: {self.hovered_index}") if self.hovered_index >= 0: combobox.removeItem(self.hovered_index) print(f"Removed item at index {self.hovered_index} from combobox.") # Reset the hovered index self.hovered_index = -1 return True # Call the base class eventFilter to handle any other events return super().eventFilter(source, event) # Run the application app = QApplication(sys.argv) window = ComboBoxApp() window.show() sys.exit(app.exec())
-
You should filter the key events on the view not the viewport.
-
@SGaist Yes it worked, instead of
if source == combobox.view().viewport():
I replaced it with:
if source == combobox.view():
And installed the filter both on the view and the viewport:
combobox.view().viewport().installEventFilter(self) combobox.view().installEventFilter(self)
-