简体   繁体   中英

Placeholder for QInputDialog

I'm trying to have a gray placeholder string inside a QInputDialog.

Wrote this one-liner after reading through the "QLineEdit Class Documentation".

    def input_keywords(self):
        keywords, result = QInputDialog.getText(self, "Input Dialog", "Enter keywords", QLineEdit.setPlaceholderText, "Lorem Ipsum")

If I use one of the other methods in the documentation like "QLineEdit.Normal" the code works.

I know that here are similar questions, but only found code written in C++.

The fourth argument of the getText static function is a QLineEdit.EchoMode flag which only controls the display of text typed into the line edit, while you tried to use a function there (which clearly cannot work).

Static functions of QDialog subclasses offer limited customization, as they are intended as convenience function that only provide the more basic and common options. They automatically create a new instance of the dialog with the given option, and there's no direct control over that instance.

The most correct way to set a placeholder for the input field is to create an instance of the dialog, set all options afterwards, and then use findChild() in order to get the QLineEdit instance of the dialog.

    def input_keywords(self):
        dialog = QtWidgets.QInputDialog(self)
        dialog.setInputMode(QtWidgets.QInputDialog.TextInput)
        dialog.setWindowTitle('Input Dialog')
        dialog.setLabelText('Enter keywords')
        lineEdit = dialog.findChild(QtWidgets.QLineEdit)
        lineEdit.setPlaceholderText('Lorem Ipsum')
        if dialog.exec_():
            print(dialog.textValue())

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