I just implemented a QLineEdit
that selects it's text right after getting focus. I created a derived class and added
virtual void focusInEvent(QFocusEvent *event) override;
to the header. I first tried to implement it like so:
void MyLineEdit::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
selectAll();
}
but it wouldn't select the text, as apparently, some stuff wasn't processed yet at the time selectAll()
is called.
The working solution is to put the selectAll()
call in a QTimer::singleShot
lambda call with 0 seconds to wait like so:
void MyLineEdit::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
QTimer::singleShot(0, [this]() { selectAll(); } );
}
This lets everything be processed before selectAll()
is invoked and everything works fine.
This is only one example, I already ran into this problem several times. So I wonder if there's a pre-defined method of telling Qt "Execute the following, but process everything else before"?
in the class define, add the code: signals: void focusIn();
in the constructor function, add the code: connect(this, &MyLineEdit::focusIn, this, &QLineEdit::selectAll, Qt::QueuedConnection);
in the focusInEvent function, add the code: emit this->focusIn();
work fine!
You could do this:
QMetaObject::invokeMethod(this, "selectAll", Qt::QueuedConnection);
It is debatable whether this is nicer though; also it only works for slots and other invokables declared with Q_INVOKABLE
and not for all methods.
Stylistically I agree with you that it would be nice to have an API for this; the QTimer::singleShot()
construct looks a bit strange (but works fine).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.