How To Use QInputDialog in PySide


Today’s goal is to create a simple dialog by using QInputDialog in PySide.


Windows 10
Python 3.8.7
PySide2 (5.15.2)


Reference: QInputDialog

Create the simplest dialog

There are 4 types of dialogs, getText, getInt, getDouble and getItem. Here is an example of QInputDialog() in

main UI to display dialogs

getText Dialog

def show_get_text_dialog(self):
    name, ok = QInputDialog.getText(self, 'Name form', 'input name:')
    if ok:
        print("input is ", name)

getInt Dialog

def show_get_int_dialog(self):
    age, ok = QInputDialog.getInt(self, "age", "input age:", minValue=0)
    if ok:
        print("input is ", age)

getDouble Dialog

def show_get_double_dialog(self):
    height, ok = QInputDialog.getDouble(self, "height", "input height:", minValue=0)
    if ok:
        print("input is ", height)

getItem Dialog

def show_get_item_dialog(self):
    selected_class, ok = QInputDialog.getItem(self, "class", "select class", ["math", "science", "history"])
    if ok:
        print("input is ", selected_class)

Customize dialog

You can customize dialog with set functions as below.

def show_save_dialog(self):
    dialog = QInputDialog()
    dialog.setLabelText("file name:")
    ok = dialog.exec_()
    file_name = dialog.textValue()
    if ok:
        print(file_name + " was saved")
        print(file_name + " was not saved")