我有 QTextBrowser 和委托(delegate)类, 在 QTextBrowser 中,我设置了带链接的 html 文本,但在这个 html 中,我的文本看起来像 像这样与 css 链接:
"<span style=\" font-size:8pt; text-decoration: underline; color:#ffffff;\">dummy_link</span>"
我喜欢将光标类型更改为当鼠标悬停在它上面时指向它。然后触发Qt函数。 问题是当我尝试在带有委托(delegate)类的 QTextBrowser 中实现时 mouseMoveEvent(QMouseEvent *e) 像这样:所有其他链接(标签)丢失那里的指针光标是当我这样做的时候:
void TextBrowserDelegate::mouseMoveEvent(QMouseEvent *e)
{
QCursor newCursor = cursor();
Qt::CursorShape CurrCursor = newCursor.shape();
QTextCursor tc = cursorForPosition( e->pos() );
tc.select( QTextCursor::WordUnderCursor );
QString sharStr = tc.selectedText();
if(sharStr == "dummy_link")
{
Qt::CursorShape newCursor = Qt::PointingHandCursor;//Qt::ArrowCursor;
setCursor(newCursor);
}
e->accept();
}
我在这里做错了什么?
最佳答案
使用您提供的代码,看起来只有带有文本“dummy_link”的链接才能获得您选择的光标。如果您设置了正确的标志,QTextBrowser 类应该会自动更改光标。
QTextBrowser::setOpenLinks(true);
如果您的 TextBrowserDelegate 继承自 QTextBrowser,您可以在构造函数中使用以下代码:
TextBrowserDelegate::TextBrowserDelegate(QWidget *parent){
this->setOpenExternalLinks(true);
this->setOpenLinks(true);
connect(this,SIGNAL(anchorClicked(QUrl)),this,SLOT(onClickedLink(QUrl)));
}
void TextBrowserDelegate::onClickedLink(QUrl url){
//do something with url
}
关于c++ - Qt QTextBrowser 如何捕获文本并更改其光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9173474/