Solved QLineEdit - Delete text when inserting another one
-
Hi,
I have a modal window with a QComboBox editable.
When I past a text to this combo, I want to delete old text and replace it by newest.
I tried this:
connect(ui.mediafileComboBox->lineEdit(), &QLineEdit::textEdited, this, &MyModal::slotTxtChange);void MyModal::slotTxtChange(const QString &text)
{
ui.myCombo->lineEdit()->clear();
ui.myCombo->lineEdit()->setText(text);
}
But I have to past 2 times my text to this code take effect.
Have you an idea?
Thanks. -
Hi,
Do you mean that you would like the whole original text to be selected when the line edit gets the focus so you can just past over it ?
-
No, because if I do this, there introduce an accessibility issue on Linux. It's why I want to use signals and slots.
But right, select text when combo gets focus is working. -
Which accessibility issue ?
-
I've an history of latest lines in my combo, on Linux if I select text of current line, Orca has some difficulties to read lines.
-
From the looks of it, you can't override paste.
Maybe an event filter on the QShortCutEvent might do the trick when dealing with the Paste shortcut. -
Do you mean something like this?
void myModal::keyPressEvent(QKeyEvent* e)
{
ui.myCombo->lineEdit()::keyPressEvent(e);if(e->matches(QKeySequence::Paste)) { slotTxtChange(); }
}
But how I can retrieve content to past to my function to delete current text and insert the newest? -
I would just call clear followed by paste.
-
QLineEdit has no function named paste, right?
Should I integrate another class? -
Sure it does: QLineEdit::paste
-
Oh yes sorry.
So I made the following:
void myModal::keyPressEvent(QKeyEvent* e)
{
if(e->matches(QKeySequence::Paste))
{
ui.myCombo->lineEdit()->clear();
ui.myCombo->lineEdit()->paste();
}
}
But I get the following error:
error: invalid use of incomplete type 'class QKeyEvent'
if(e->matches(QKeySequence::Paste))
^~
sorry I'm not very good at handling QKeyEvent -
You did not include the corresponding header.
-
Oh, yes, right.
Latest thing, now it's working, but all other keys are without any effect.
What should I add to let QT use other keys as expected? -
Call the base class implementation for all the other cases.
-
Oh it was note latest thing :), so now I have this line added after my condition:
QLineEdit::keyPressEvent(e);
but I give the following error:
error: cannot call member function 'virtual void QLineEdit::keyPressEvent(QKeyEvent*)' without object -
You cannot call the base class implementation of a different class.
Note that I suggested to use an event filter not to override the keyPressEvent of a different class.
-
OK I understood. So I made this:
bool myModal::eventFilter(QObject *object, QEvent *event)
{
if (object == ui.myCombo->lineEdit() && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->matches(QKeySequence::Paste))
{
ui.myCombo->lineEdit()->clear();
ui.myCombo->lineEdit()->paste();
return true;
} else
return false;
}
return false;
}but I still have to press CTRL+V twice to have this code works as expected :(
-
What if you just call clear and then return false ?
-
I found. It's working without the ->lineEdit() after ui.myCombo.
Thanks for your help.