我做了这个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/