Two inputs from QInputDialog in QtWidgets

text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')

enter image description here

With this code that i share, i can only get one input from user. Is there any way to get two inputs from QInputDialog in QtWidgets? I dont want to create a new window for this situation. All i want is that get two inputs from QInputDialog if it's possible.

Upvotes: 0

Views: 1133

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

You have to inject the QLineEdit to the QInputDialog:

import sys
from PyQt5 import QtWidgets


def get_text_values(initial_texts, parent=None, title="", label=""):
    dialog = QtWidgets.QInputDialog()
    dialog.setWindowTitle(title)
    dialog.setLabelText(label)
    dialog.show()
    # hide default QLineEdit
    dialog.findChild(QtWidgets.QLineEdit).hide()

    editors = []
    for i, text in enumerate(initial_texts, start=1):
        editor = QtWidgets.QLineEdit(text=text)
        dialog.layout().insertWidget(i, editor)
        editors.append(editor)

    ret = dialog.exec_() == QtWidgets.QDialog.Accepted
    return ret, [editor.text() for editor in editors]


def main():
    app = QtWidgets.QApplication(sys.argv)
    ok, texts = get_text_values(
        ["hello", "world"], title="Input Dialog", label="Enter your name:"
    )
    print(ok, texts)


if __name__ == "__main__":
    main()

In your case:

ok, texts = get_text_values(
    ["hello", "world"], title="Input Dialog", label="Enter your name:", parent=self
)
print(ok, texts)

Upvotes: 1

Related Questions