Important: Please read the Qt Code of Conduct -

How to have GUI based program wait until user enters acceptable input

  • I have a text edit in my gui that a user will enter a value into it. If the value is not entered, I want to show a popup message, tell them that it is empty, and then have them re-enter it. Right now what's happening is that the popup message is being displayed, but then the user does not have the chance to change the value, the program just continues running and crashes.

    I have a 'connect' push button which is the trigger for all of this that is called pb_ip_connection

    def get_ip():
        ip_addr = self.ui.text_edit_ip.toPlainText()
        if ip_addr == "":
            ctypes.windll.user32.MessageBoxW(0, "Enter an IP address", "ERROR", 1)
            # something has to go here to make the program go back and look for the new IP address that the user is going to enter before moving forward

    I am also aware that I can do something along the lines of

    while True: 
        ip_addr = self.ui.text_edit_ip.toPlainText()
        if ip_addr == "":
            ctypes.windll.user32.MessageBoxW(0, "Enter an IP address", "ERROR", 1 

    That would work if the user was entering an input, but since this is gui driven it just shows the popup over and over again and does not give the user a chance to change anything

  • Lifetime Qt Champion

    Use a QLineEdit instead a QTextEdit, connect to signal QLineEdit::textChanged() and check the input in the connected slot. Or/And put a QValidator on it so the user can not enter e.g. characters.

  • @poordev123
    As @Christian-Ehrlicher says, except that you may rather want to use signal QLineEdit::editingFinished().

    Completely separate from your question, it seems such a shame that you use ctypes.windll.user32.MessageBoxW() to display a message box. If you are going to use Qt why use anything like that? Anyway, QMessageBox Class.

  • @JonB Jon has a good point. You code will not compile on Linux and MacOS. If your app is not developed for cross-platforms, Qt is not needed on Windows.

  • @JonB @JoeCFD Okay I just switched to using QMessageBox then, I had not thought about running this on different operating systems. Thank you for the tip.

    @Christian-Ehrlicher thank you for the textChanged input. I am going to try to implement that

Log in to reply