Reputation: 91
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++.
Upvotes: 0
Views: 629
Reputation: 48231
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())
Upvotes: 2