[SOLVED] Constantly updating tooltip text without moving the mouse



  • Hi,

    is there a way to update the text of a tooltip which is already visible without any extra event (no mouse movement).

    Thanks



  • I haven't tried it, but couldn't you create a timer when you get the tooltip event and use the timer to update the tooltip?



  • I update the tooltip in the painEvent but it seems that it rendered only once so the updates are not displayed.



  • Here's a simple example of how it can work.

    Find the tooltip example.

    To Sortingbox.h, add:

    private:
    QPoint pos;

    To Sortingbox.cpp, add:
    void SortingBox::update()
    {
    static int ncalls = 0;
    QString tt;
    tt = QString("Tooltip #%1").arg(ncalls++);
    QToolTip::showText(pos, tt);
    }
    And change the event method to this:
    bool SortingBox::event(QEvent *event)
    {
    if (event->type() == QEvent::ToolTip) {
    QHelpEvent *helpEvent = static_cast<QHelpEvent *>(event);
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    timer->start(500);
    pos = helpEvent->globalPos();
    int index = itemAt(helpEvent->pos());
    if (index != -1) {
    QToolTip::showText(helpEvent->globalPos(), shapeItems[index].toolTip());
    } else {
    QToolTip::hideText();
    event->ignore();
    }

        return true;
    }
    return QWidget::event(event);
    

    }

    The tooltip for items will update constantly (every 1/2 second) with "Tooltip #x" with x incrementing from 0.



  • Sorry about the formatting.


  • Moderators

    [quote author="ScottR" date="1390323723"]Sorry about the formatting.[/quote]

    Just put your code between '@' tags and it will be fine :)



  • Thanks for the code but I found another way. This works because I am changing the tooltips text every time my values change so I listen to QEvent::ToolTipChange. This leads to the following code:

    @
    bool MyClass::event(QEvent *ev)
    {
    if (ev->type() == QEvent::ToolTip)
    {
    QHelpEvent *helpEvent = static_cast<QHelpEvent *>(ev);
    m_ToolTipPos = helpEvent->globalPos();
    }
    else if (ev->type() == QEvent::ToolTipChange)
    {
    if (underMouse() && !m_ToolTipPos.isNull())
    QToolTip::showText(m_ToolTipPos, toolTip());
    }
    return QWidget::event(ev);
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.