Important: Please read the Qt Code of Conduct -

Tooltip according to text background color or some other idea?

  • Dear all,

    I am creating a main window and then putting a textbox there in my form. and then this is what I am trying to do as it was mentioned "here": and I am not getting any tooltip.

    if (event->type() == QEvent::ToolTip)
    QHelpEvent* helpEvent = static_cast <QHelpEvent*>(event);
    QTextCursor cursor = ui->recvEdit->cursorForPosition(helpEvent->pos());;
    if (cursor.selectedText() == "CHECK")
    QToolTip::showText(helpEvent->globalPos(), cursor.selectedText());

    Also is it possible that i can use compare the font color or background color of my word instead of word.

    [Edit: Fixed URL in link and code formatting -- mlong]

  • Do you get any text at all from the cursor? Are the coordinates correct? You will probably need to translate from global to local coordinates.

  • Thank you very much for the reply.
    No I am not getting any text as tooltip.
    I think they are correct because I am taking the current position of the cursor according to code.
    If you mean instead of passing in
    @ QToolTip::showText(helpEvent->globalPos(), cursor.selectedText());}@

    i should try passing local then i also did that but same result.

    here is the original code from where I am getting the idea. Maybe it will give you better understanding that what I am trying to achieve here.

    @#include <QtGui>

    class TextEdit : public QTextEdit
    TextEdit(QWidget *parent) : QTextEdit(parent)
    setText("This application only provides a tooltip for the following word: polymorphism");
    QPalette pal = palette();
    pal.setColor(QPalette::Base, QColor(1, 0.941176, 0.941176, 0.941176) );
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

        bool event(QEvent* event) {
                if (event->type() == QEvent::ToolTip)
                        QHelpEvent* helpEvent = static_cast <QHelpEvent*>(event);
                        QTextCursor cursor = cursorForPosition(helpEvent->pos());
                        if (cursor.selectedText().startsWith("po" ))
                            QToolTip::showText(helpEvent->globalPos(), "Polymorphism");
                        return true;
                return QTextEdit::event(event);


    What i want is to use this
    @bool event(QEvent* event) {}@

    function in my code where i've already a textedit using form ui.
    Thanks in advance for the help.

Log in to reply