Solved QComboBox with QCompleter, lineedit -> selectAll
-
Hi all,
For a large dropdown (250+ items) I used QCompleter for a searchable QCombobox. Searching and autocompletion works fine, but now I want to select all text when I click in the textfield.
As far as I correctly understand http://doc.qt.io/qt-5/qlineedit.html#selectAll , the text is highlighted and deleted if I start typing.
To use it I installed an event filter on the QLineEdit
//thiscombo is a valid QWidget QLineEdit *le = new QLineEdit(thiscombo); le->setObjectName("Completer_1206"); le->installEventFilter(this); apcompleter = new QCompleter(le); connect(apcompleter,SIGNAL(activated(QString)),this,SLOT(completerslot(QString))); thiscombo->setLineEdit(le); thiscombo->setAutoCompletion(false); thiscombo->setInsertPolicy(QComboBox::NoInsert); le->setCompleter(apcompleter); apcompleter->setCaseSensitivity(Qt::CaseInsensitive); apcompleter->setCompletionMode(QCompleter::PopupCompletion); apcompleter->setModel(new QStringListModel(thiscombo));
//inside the ::eventFilter(QObject,QEvent) if(object->objectName() == "Completer_1206" && event->type() == QEvent::MouseButtonPress) { qDebug() << object; QLineEdit *l = dynamic_cast<QLineEdit *>(object); l->selectAll(); qDebug() <<l->selectedText() << l->hasSelectedText(); }
So on MouseClick I get the selectedText and hasSelectedText is true, but the text is not highlighted, and when i start typing, the text is inserted at the cursor position.
Is there anything wrong in the way I try to highlight the text?
Thanks in advance
-
Well the solution is very simple.
The last line in my eventFilter is
QWidget::eventFilter(object,event);
So it also does what the widget usually does on mouse click.
a simple
return true;
at the end of my
if
and the text is highlighted.