简体   繁体   中英

PyQt5 QInputDialog not showing

I am playing around with extending the QInputDialog, which I want to open when the user presses a certain shortcut. I can see the shortcut run and the code runs correctly past the show() method but the QInputDialog is never shown.

This only happens when I try to open the QInputDialog through a shortcut, if I just put the QInputDialog in my main method, it runs fine.

class CommandPopup(QInputDialog):
""" popup for a single-line command to be entered"""
def __init__(self):
    super().__init__()
    self.setupGUI()
    self.command_runner = commands.CommandRunner()

def setupGUI(self):
    self.setLabelText("Command:")
    self.show()

def done(self, result):
    super().done(result)
    print("done")
    if result == 1:
        print(self.textValue())
        self.command_runner.run(self.textValue())

This works when I put this in my main function

if __name__ == '__main__':
app = QApplication(sys.argv)
ui = CommandPopup()
sys.exit(app.exec_())

But when I try to call the code from another function on a shortcut, it does not show the input dialog.

         self.textArea.shortcut = QShortcut(QKeySequence("CTRL+E"),self)
         self.textArea.shortcut.activated.connect(self.command_popup)

with:

 def command_popup(self):
    x = CommandPopup()

(SO messed up the indentation a bit, but the indentation is correct, I can see string output if I print something after the self.show() method.

You must pass a parent to the object. To do this we must modify the constructor by adding that parameter.

class CommandPopup(QInputDialog):
    """ popup for a single-line command to be entered"""
    def __init__(self, parent=None):
        super().__init__(parent=parent)
        [...]

def command_popup(self):
    print("print")
    command = CommandPopup(self)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM