how to create tooltip for highlighted strings in QPlainTextEdit
Asked Answered
L

2

8

I have a QPlainTextEdit and have some words highlighted in it. Now, when I hover over it with the mouse, I want it to show me a tooltip that has a description or something like that about this highlighted word. Something like this in the Qt IDE:

enter image description here

but I don't know how to start this so any idea, code or similar project to check would be helpful.

Leonteen answered 8/3, 2017 at 17:26 Comment(0)
R
6

For this case I will create a class that inherits from QPlainTextEdit, reimplement the event() method and enable mouse tracking with setMouseTracking()

plaintextedit.h

#ifndef PLAINTEXTEDIT_H
#define PLAINTEXTEDIT_H

#include <QPlainTextEdit>

class PlainTextEdit : public QPlainTextEdit
{
public:
    PlainTextEdit(QWidget *parent=0);

    bool event(QEvent *event);
};

#endif // PLAINTEXTEDIT_H

plaintextedit.cpp

#include "plaintextedit.h"
#include <QToolTip>


PlainTextEdit::PlainTextEdit(QWidget *parent):QPlainTextEdit(parent)
{
    setMouseTracking(true);
}

bool PlainTextEdit::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().isEmpty())
            QToolTip::showText(helpEvent->globalPos(), /*your text*/QString("%1 %2").arg(cursor.selectedText()).arg(cursor.selectedText().length()) );

        else
            QToolTip::hideText();
        return true;
    }
    return QPlainTextEdit::event(event);
}

Complete Code: Here

Rapp answered 8/3, 2017 at 18:3 Comment(2)
Thank you for this really helpful answer but is there any way to select just highlight word? if there any option cursor.select(QTextCursor::Highlight); or something like thatLeonteen
@Leonteen If you don't mind translating it back from Python, I did that and more in this answer.Portable
H
4

@eyllanesc's answer is great, but I would like to add that if you have viewport margins set, the position must be adjusted because otherwise it will be offset, and an incorrect cursor position reported.

The doc for cursorForPosition() states

returns a QTextCursor at position pos (in viewport coordinates). emphasis added

bool PlainTextEdit::event(QEvent *event)
{
    if (event->type() == QEvent::ToolTip)
    {
        QHelpEvent* helpEvent = static_cast<QHelpEvent*>(event);
        
        QPoint pos = helpEvent->pos();
        pos.setX(pos.x() - viewportMargins().left());
        pos.setY(pos.y() - viewportMargins().top());
        
        QTextCursor cursor = cursorForPosition(pos);
        cursor.select(QTextCursor::WordUnderCursor);
        if (!cursor.selectedText().isEmpty())
            QToolTip::showText(helpEvent->globalPos(), /*your text*/QString("%1 %2").arg(cursor.selectedText()).arg(cursor.selectedText().length()) );

        else
            QToolTip::hideText();
        return true;
    }
    return QPlainTextEdit::event(event);
}
Hebe answered 10/1, 2020 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.