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":http://qt-project.org/faq/answer/how_can_i_display_a_tooltip_over_only_one_word_in_a_qlabel and I am not getting any tooltip.

    @
    ui->recvEdit->append("CHECK\n");
    if (event->type() == QEvent::ToolTip)
    {
    QHelpEvent* helpEvent = static_cast <QHelpEvent*>(event);
    QTextCursor cursor = ui->recvEdit->cursorForPosition(helpEvent->pos());
    cursor.select(QTextCursor::WordUnderCursor);
    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
    {
    Q_OBJECT
    public:
    TextEdit(QWidget *parent) : QTextEdit(parent)
    {
    setTextInteractionFlags(Qt::TextBrowserInteraction);
    setFrameStyle(QFrame::NoFrame);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    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) );
    setReadOnly(true);
    setPalette(pal);
    setFixedHeight(18);
    setFixedWidth(400);
    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());
                        cursor.select(QTextCursor::WordUnderCursor);
                        if (cursor.selectedText().startsWith("po" ))
                            QToolTip::showText(helpEvent->globalPos(), "Polymorphism");
                        else
                                QToolTip::hideText();
                        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
 

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