Search code examples
pythonpyqtpyside

change the default display message of QKeySequenceEdit?


enter image description here

when adding a QKeySequenceEdit, theres this default display message "Press shortcut". is there any way to change or delete it? i tried to look for here (https://doc.qt.io/qt-6/qkeysequenceedit-members.html) but it doesnt seem to provide any options for it.


Solution

  • I had this same problem and found a way to deal with it after reading the answer from @SebDieBln. I am not a programmer, so this is probably not the most elegant solution but it worked for my needs, at least.

    I have a separate settings class where I handle changing hotkeys, so I applied Sebs suggestion where I added the QKeySequenceEdit:

    self.hotkey_edit = QKeySequenceEdit(self)
    line_edit = self.hotkey_edit.findChild(QLineEdit, "qt_keysequenceedit_lineedit")
        if line_edit:
            line_edit.setPlaceholderText("Enter custom Placeholdertext here")
    

    I was already using an eventFilter to intercept other events, so I installed it on the settings class upon creation in my main window class:

    self.settings_dialog.installEventFilter(self)
    

    and made it listen for changes on the QKeySequenceEdit, and if the text is cleared, reset the placeholder text:

    def eventFilter(self, obj, event):
        if hasattr(self, 'settings_dialog') and self.settings_dialog:
            if obj == self.settings_dialog.hotkey_edit and event.type() == QEvent.Type.KeyRelease:
                line_edit = self.settings_dialog.hotkey_edit.findChild(QLineEdit, "qt_keysequenceedit_lineedit")
                if line_edit and not line_edit.text():
                    line_edit.setPlaceholderText("Enter custom Placeholdertext here")
    
        return super().eventFilter(obj, event)