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 ?
-
Which accessibility issue ?
-
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.
-
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.
-
Call the base class implementation for all the other cases.
-
-
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 ?