QLineEdit: selectAll() selects text but does not show any visible selection (Embedded Linux)
-
Hi all,
I have a problem regarding the QLineEdit in an embedded linux application:
I want to programatically set the default text in a QLineEdit and also have it selected and as such be visibly selected (e.g marked with "selection-background-color" and "selection-color" as defined in the style sheet).
So, I use the selectAll() function of the QLineEdit.The problem is that the text seems to be selected, as the hasSelectedText() function returns true and the selectedText() returns the correct text. However, the text is not marked as selected.
If I then (via the touch screen) select the QLineEdit the text appears selected. BUT, if I then start to type the default text
does not get deleted but the new text is appended at the end. Shouldn't the marked text get deleted by default in this case?I have looked at and used mr. Volker's suggestion as described here:
"http://developer.qt.nokia.com/forums/viewthread/8590":http://developer.qt.nokia.com/forums/viewthread/8590
but the problem still remains.
So, my questions is: How do I programatically set a default text in a QLineEdit and make it selected in code and visibly selected to the user without having to manually (via mouse, keyboard or touchscreen) select the QLineEdit??
Best Regards,
Bubbas -
Did you find a solution? I have the same problem on not embedded Linux (Ubuntu 11.10, Gnome 3).
@
_view.label->installEventFilter(this);
@@
bool SecretForm::eventFilter(QObject* obj, QEvent* event) {
if (obj == _view.label) {
if (event->type()== QEvent::FocusIn) {
if (_view.label->text() == DEFAULT_SECRET_LABEL) {
_view.label->selectAll();
qDebug() << _view.label->hasSelectedText();
//_view.label->setSelection(0,_view.label->text().length()-1);
//return true;
}
}
}
//return true;
}
@Just prints: "true". No selection visible.
-
All you need to do is to put those lines at the end, after you set all other widgets..
lineEdit->selectAll()
lineEdit->setFocus() -
I know this thread is old, but some of us are still stuck with QT 4.8.4 (QWS) in embedded. I spent some time trying to figure out how to selectAll on a lineEdit that was setup via a UI file.
I tried the following, but it didn't work . ( note it does work when I build for desktop )
ui->lineEdit->selectAll();
ui->lineEdit->setFocus();
Then I noticed If I pressed the line edit then ran the selectAll() it worked.
So if you force a mouse press event for the widget, it forces focus and hence will let you selectALL().
QEvent eventPress( QEvent::MouseButtonPress );
QEvent eventRelease( QEvent::MouseButtonRelease );
QApplication::sendEvent( ui->lineEdit , &eventPress ); // simulate a mouse press
QApplication::sendEvent( ui->lineEdit , &eventRelease ); // simulate a mouse release
ui->lineEdit->selectAll();This worked for me, I hope it can get someone else out of a bind too.
I'm posting this because I didn't see any other examples of people with this work around.