c++ - QTextEdit:获取鼠标指针下的单词

标签 c++ qt qt-creator qtextedit

我创建了这个 CompileOutput 类,基本上我想要的是当我点击单词时,它发出信号 selectedWord,当我将鼠标悬停在单词上时,我想要它发出信号 hoveredWord

mousePressEvent 有效,但 enterEvent 无效。当我将鼠标悬停在文字上时,没有任何反应。

这是我的代码块:

CompileOutput::CompileOutput(QWidget *parent): QTextEdit(parent)
{
    setReadOnly(true);
}
CompileOutput::~CompileOutput()
{
}

void CompileOutput::mousePressEvent(QMouseEvent * event)
{
    QTextCursor tc = cursorForPosition ( event->pos());
    tc.select(QTextCursor::LineUnderCursor);
    QString strWord = tc.selectedText();

    if(!strWord.isEmpty())
    {

        emit selectedWord(strWord);
    }
}

void CompileOutput::enterEvent(QMouseEvent *event)
{
    QTextCursor tc = cursorForPosition(event->pos());
    tc.select(QTextCursor::LineUnderCursor);
    QString strWord = tc.selectedText();

    qDebug() << strWord;
    if(strWord=="the line i need.")
    {
        emit hoveredWord(); //this signal makes cursor shape change while over right line of text
    }

}

最佳答案

试试这个:

在构造函数中添加

setMouseTracking(true);

然后使用 mouseMoveEvent 而不是 enterEvent

关于c++ - QTextEdit:获取鼠标指针下的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40920211/

相关文章:

c++ - 如果我打开文件后有人覆盖该文件会怎样?

c++ - R: C 符号不在加载表中

C++删除char中每个x的x字节

c++ - 为什么 Qt 将 'PWD' 添加到未相对引用的库中?

c++ - C++成员函数自定义类型返回 'does not name a type'错误

qt - 使用来自组合框的信号禁用 Pyqt 中的 QspinBox

c++ - 为什么在 sqlite - QT here - 中触发查询失败?

qt - 如何找到日期的日期

qt - qmake 子目录模板同时运行所有应用程序

qt - 如何在构建 qmake 子文件夹模板后安装第一个项目?