Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Trying to delete QComboBox items while hovering over them and pressing the "delete" key.
Forum Updated to NodeBB v4.3 + New Features

Trying to delete QComboBox items while hovering over them and pressing the "delete" key.

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 367 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mizmas
    wrote on last edited by Mizmas
    #1

    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)
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which version of PySide/PyQt are you using ?
      On which OS ?
      Can you make your example complete so it's runnable ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Which version of PySide/PyQt are you using ?
        On which OS ?
        Can you make your example complete so it's runnable ?

        M Offline
        M Offline
        Mizmas
        wrote on last edited by
        #3

        @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())
        
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You should filter the key events on the view not the viewport.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          1
          • SGaistS SGaist

            You should filter the key events on the view not the viewport.

            M Offline
            M Offline
            Mizmas
            wrote on last edited by
            #5

            @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)
            
            1 Reply Last reply
            0
            • M Mizmas has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved