1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] PySide2: QListWidget Popup Code Completion and Focus Management Issues

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024 às 05:52.

  1. Stack

    Stack Membro Participativo

    I'm trying to build an IntelliSense-like code completion system inside a QPlainTextEdit using PySide2. However, I'm facing issues with focus management and hiding the QListWidget when it pops up. The goal is for the popup to appear when the user is typing, allowing them to continue typing into the text editor while the popup is visible, and then hide the popup when needed (e.g., pressing the Escape key or moving the cursor away).

    Here are the main issues I'm facing:

    Focus Issue: I'm using self.completer_list.setFocusPolicy(Qt.NoFocus) to prevent the popup from taking focus, but when using Qt.Popup, the focus still shifts to the QListWidget, and the rest of the program becomes unresponsive until I interact with the popup.

    Popup Hiding Issue: I'm using self.completer_list.hide() to hide the popup, but it doesn't always hide properly or doesn't respond well when clicking outside or when the cursor moves.

    What might I be doing wrong in my code?

    This is part of my code:

    self.temp_completer_list = ["for", "while", "if", "else", "elif", "def",
    "class", "import", "from", "return", "try",
    "except", "finally"]
    self.completer_list = QListWidget()
    self.completer_list.addItems(self.temp_completer_list)
    self.completer_list.setWindowFlag(Qt.Popup)
    self.completer_list.hide()
    self.textChanged.connect(self.show_completer_list)

    def show_completer_list(self):
    """CURSOR TO POPUP"""
    cursor = self.textCursor()
    pos = self.cursorRect(cursor).bottomRight()
    global_pos = self.mapToGlobal(pos)
    self.completer_list.move(global_pos)
    self.completer_list.show()
    self.completer_list.setFocusPolicy(Qt.NoFocus)

    Continue reading...

Compartilhe esta Página