selectAll in QLineEdit does not work
I have subclassed QLineEdit in order to handle a focus event, and I would like all existing text to be highlighted as soon as I click in the textbox. The selectAll() method does not work. What am I missing?
Code:
// --------------------------------------------------------------------
{
Q_OBJECT
protected:
};
this->selectAll(); // THIS DOESN'T WORK
//this->clear(); // <-- THIS WORKS!
}
Re: selectAll in QLineEdit does not work
It most likely is working. What your getting is that you are selecting all the text, and then click a character in the box with your mouse, and so clearing the selection. Try it yourself - select all the text and then click in the box with your mouse.
One solution would be to wait until the mouse button is released before doing the selectAll. Another would be to use a timer.
Re: selectAll in QLineEdit does not work
Hmm, I didn't quite understand your answer. I must be missing something. Here's a complete example. I would like the text to be highlighted as soon as I click in the textbox.
Code:
#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H
#include <QtGui>
{
Q_OBJECT
protected:
};
#endif // MYLINEEDIT_H
Code:
#include "mylineedit.h"
//this->clear();
this->selectAll();
}
Code:
#include "mylineedit.h"
#include <QtGui>
int main(int argc, char *argv[])
{
MyLineEdit *tmp = new MyLineEdit();
tmp->setText("test this");
flayout->addWidget(tmp);
f->setFocus();
f->show();
return app.exec();
}
Re: selectAll in QLineEdit does not work
I would surely call the base class implementation of the event handler as it does some useful things.
Code:
this->selectAll();
}
If you want to select contents of the line edit upon receiving focus (and focusInEvent doesn't work, remember about the base class implementation call!) then connect QApplication::focusChanged() signal to a custom slot, check if the widget receiving the focus is your line edit and call selectAll() on it.
Re: selectAll in QLineEdit does not work
Hi, in case someone comes across this thread, this solution worked for me:
Code:
{
QTimer::singleShot(0,
[this](){ selectAll();
});
}