Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QToolTip not showing when QLineEdit is focused



  • Hey,

    I have Subclassed QLineEdit and added focusInEvent and focusOutEvent like this

    void QLineEditSubclass::focusInEvent(QFocusEvent *e)
    {
        QLineEdit::focusInEvent(e);
        emit(focused(true));
    }
    
    void QLineEditSubclass::focusOutEvent(QFocusEvent *e)
    {
        QLineEdit::focusOutEvent(e);
        emit(focused(false));
    }
    

    Focus slot is working, but QToolTip is not showing.

    void Class::on_lineEdit_focused(bool focused)
    {
        QPoint pos = mapToGlobal(ui->lineEdit->pos());
        qDebug() << pos;
        QToolTip::showText(pos, "Test text");
        qDebug() << QToolTip::text();
        qDebug() << QToolTip::isVisible();
    }
    

    QDebug is printing pos and text of the QToolTip. isVisible returns false and QToolBox itself is not showing.


  • Lifetime Qt Champion

    Hi,

    Why not call setToolTip on your widget ?



  • @SGaist
    It shows tooltip only if mouse cursor is on the widget. I forgot to mention that tooltip should be shown even without cursor.



  • try adding the widget to the tooltip call QToolTip::showText(pos, "Test text",this);

    From the docs:

    The rect is in the coordinates of the widget you specify with w. If the rect is not empty you must specify a widget. Otherwise this argument can be 0 but it is used to determine the appropriate screen on multi-head systems.



  • @VRonin
    Tried

    QToolTip::showText(pos, "Test text", this);
    

    and

    QToolTip::showText(pos, "Test text", ui->lineEdit);
    

    without any success.


  • Lifetime Qt Champion

    The QToolTip might not be the best tool then. Why do you need a permanent QToolTip ?



  • @SGaist
    I need to have tooltip when qlineedit is focused to inform user about acceptable datatype, value range and small description.
    I think I could make a special widget for this, which acts like tooltip. It could change location, text and visibility based on focused widget.



  • @Kamba

    Hi.

    something like this:

    QLineEditSubclass::QLineEditSubclass(QWidget *parent)
    : QLineEdit(parent)
    {
        setToolTip("Test text");
    }
    
    void QLineEditSubclass::focusInEvent(QFocusEvent *e)
    {
        QLineEdit::focusInEvent(e);
        
        auto    gPos = (parentWidget()) ? parentWidget()->mapToGlobal(pos()) : pos();
        auto    toolTipEvent = new QHelpEvent(QEvent::ToolTip, pos(), gPos);
        QApplication::postEvent(this, toolTipEvent);
    }
    

    Not tested... :)


  • Lifetime Qt Champion

    @Kamba: Looks like a QWidget with a QLineEdit on top of a QLabel and you set your text on the QLabel. You can style it if you have special color needs.


Log in to reply