Showing tooltip of QLineEdit upon focus in Qt
I found that I can set a tooltip on a QLineEdit as such:
equation = new QLineEdit();
equation->setToolTip("Example: a*b+c+~c");
However, I would like the tooltip to be displayed when tha开发者_开发技巧t QLineEdit is focused. How do I do that?
Thanks in advance.
I was able to accomplish this by subclassing QLineEdit and overriding focusInEvent(...) as such:
void EquationEditor::focusInEvent(QFocusEvent *e)
{
QHelpEvent *event = new QHelpEvent(QEvent::ToolTip,
QPoint(this->pos().x(), this->pos().y()),
QPoint(QCursor::pos().x(), QCursor::pos().y()));
QApplication::postEvent(this, event);
QLineEdit::focusInEvent(e);
}
I would suggest that you have a look at the following example : Tool Tips Example
You could show the tooltip when your LineEdit is getting the focus, maybe by connecting to this signal:
void QApplication::focusChanged ( QWidget * old, QWidget * now ) [signal]
There is also some pretty neat informations about Focus here : QFocusEvent Class Reference
Hope it helps a bit !
精彩评论