在Qt中,当QLineEdit获得焦点时显示提示信息

3

我发现可以通过以下方式在QLineEdit上设置工具提示:

equation = new QLineEdit();
equation->setToolTip("Example: a*b+c+~c");

然而,我希望当焦点在QLineEdit上时显示工具提示。我该怎么做呢?
提前感谢。

我已经有了一些进展。目前为止,我的代码如下:void EquationEditor::focusInEvent(QFocusEvent *e) { QHelpEvent event(QEvent::ToolTip, this->pos(), this->pos()); QApplication::sendEvent(this, &event); QLineEdit::focusInEvent(e); }但是我不确定如何设置 QHelpEvent 的最后两个参数。 - Gezim
2个回答

1

我能够通过子类化QLineEdit并重写focusInEvent(...)来实现这一点:

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);
}

0
我建议您查看以下示例:工具提示示例
当LineEdit获得焦点时,您可以通过连接到此信号来显示工具提示:
void QApplication::focusChanged ( QWidget * old, QWidget * now )   [signal]

这里还有一些关于Focus的非常好的信息:QFocusEvent Class Reference

希望它能对你有所帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接