[英]Qt QTextBrowser How to capture text and change its cursor
我有带有委托类的QTextBrowser,在QTextBrowser中,我设置了带有链接的html文本,但是在此html中,我的文本看起来像是带有CSS的链接,如下所示:
"<span style=\" font-size:8pt; text-decoration: underline; color:#ffffff;\">dummy_link</span>"
我喜欢将光标类型更改为鼠标悬停时指向的类型。 然后触发Qt函数。 问题是,当我尝试使用委托类在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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.