Prevent Enter key from clear my text in QLineEdit
-
Hi
So when user press enter he is not allowed to edit the text anymore?
You could just set it to ReadOnly. -
Hi
So when user press enter he is not allowed to edit the text anymore?
You could just set it to ReadOnly. -
@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.
-
@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. -
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); }
-
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 `?
-
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.
-
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.