pythonpyqtpyqt5qcompleterqinputdialog

How to use QCompleter with an InputDialog?


I am writing a Python Application where the user can enter a String in an QInputDialog. How can i use the QCompleter to make Inputs easier?

I've already been searching on different websites and read the doc from https://doc.qt.io/qt-5/qcompleter.html#details but couldn't find any help for this problem.

To me, it seems like the QCompleter is only available for QLineEdit and QComboBox. (Please proof me wrong)

ian, okPressed = QInputDialog.getText(self, "IAN", "Please enter IAN:")

It would help me a lot if anyone could show me some code examples how to deal with this problem.

If it's not possible to use the QCompleter within the QInputDialog, do you guys have an idea for a workaround?

Much thanks =)


Solution

  • There are 2 possible solutions:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(Widget, self).__init__(parent)
    
            button = QtWidgets.QPushButton("Press me", clicked=self.onClicked)
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(button)
    
        @QtCore.pyqtSlot()
        def onClicked(self):
            QtCore.QTimer.singleShot(0, self.onTimeout)
            ian, okPressed = QtWidgets.QInputDialog.getText(
                self, "IAN", "Please enter IAN:"
            )
    
        @QtCore.pyqtSlot()
        def onTimeout(self):
            dialog = self.findChild(QtWidgets.QInputDialog)
            if dialog is not None:
                le = dialog.findChild(QtWidgets.QLineEdit)
                if le is not None:
                    words = ["alpha", "omega", "omicron", "zeta"]
                    completer = QtWidgets.QCompleter(words, le)
                    le.setCompleter(completer)
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = Widget()
        w.resize(320, 240)
        w.show()
        sys.exit(app.exec_())
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Widget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(Widget, self).__init__(parent)
    
            button = QtWidgets.QPushButton("Press me", clicked=self.onClicked)
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(button)
    
        @QtCore.pyqtSlot()
        def onClicked(self):
            dialog = QtWidgets.QInputDialog(self)
            dialog.setWindowTitle("IAN")
            dialog.setLabelText("Please enter IAN:")
            dialog.setTextValue("")
            le = dialog.findChild(QtWidgets.QLineEdit)
            words = ["alpha", "omega", "omicron", "zeta"]
            completer = QtWidgets.QCompleter(words, le)
            le.setCompleter(completer)
    
            ok, text = (
                dialog.exec_() == QtWidgets.QDialog.Accepted,
                dialog.textValue(),
            )
            if ok:
                print(text)
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        w = Widget()
        w.resize(320, 240)
        w.show()
        sys.exit(app.exec_())