Solved Prevent Enter key from clear my text in QLineEdit
-
I have a QLineEdit where user will enter numbers and I will have a slot to catch the
editingFinished
signal when something has been entered. I want the number to stay and prevent user from clearing the text box on pressing the Enter key. -
Hi
So when user press enter he is not allowed to edit the text anymore?
You could just set it to ReadOnly. -
@mrjj They can edit, I just don't want to the text to disappear after enter.
-
@lansing
But i just tested oneQPlainEdit.LineEdit
Mine does not clear the text pressing enter.. ? -
@mrjj said in Prevent Enter key from clear my text in QLineEdit:
QPlainEdit
I'm using QLineEdit, not QPlainEdit. I have set a QDoubleValidator to limit the range to 0-120. When I enter 150 and pressed enter, the text remain, but when I entered 10, the text was cleared.
-
Yep my bad i did mean LineEdit.
Can you show how you set it up ?
update:
i tried with
QDoubleValidator *myDblVal = new QDoubleValidator(0, 120, 2, this);
ui->lineEdit->setValidator(myDblVal);
and i cant get it to clear the text so not sure why yours does. -
I have set the lineEdit default to readonly in the ui, and unlock it only when mode is equal to 1 set somewhere in the file.
// at the top connect(m_ui.myLineEdit, &QLineEdit::editingFinished, this, &MyDiag::slotSetNumber); // somewhere below void MyDiag::slotSetNumber() { if (!m_ui.myLineEdit->isReadOnly()) { m_ui.myLineEdit->setReadOnly(true); } if (mode === 1) { m_ui.myLineEdit->setReadOnly(false); numText = QVariant(30).toString(); } m_ui.myLineEdit->setText(numText); }
-
Hi
I was wondering if mode is not 1 then
you call
m_ui.myLineEdit->setText(numText);
anyway and if numText is empty at that point in time, the lineEdit will be empty.Could it be something like that `?
-
I edited the first if statement on last post, it should be
if (!something)
with the "!" in front.The mode if statement does passed because I won't be able to type into it if it's false.
-
Hi
But are you sure
m_ui.myLineEdit->setText(numText);
not sets empty text ?You could try
if (!numText.isEmpty())
m_ui.myLineEdit->setText(numText);and see. else i have no guesses on why text is cleared.
-
Oh you're right, I just double checked, the numText line was wrote outside the mode if statement, moving it back inside the if statement solved it.