user63898
user63898

Reputation: 30925

Qt QTextBrowser How to capture text and change its cursor

I have QTextBrowser with delegate class , in the QTextBrowser i set html text with links , but in this html i have text that looks like link with css like this:

"<span style=\" font-size:8pt; text-decoration: underline; color:#ffffff;\">dummy_link</span>"

i like to change the cursor type to point when the mouse over it . and then trigger Qt function . the problem is that when i try to implement in the QTextBrowser with delegate class the mouseMoveEvent(QMouseEvent *e) like this : all other links ( tags )loss there pointer cursors here is when i do :

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();

}

what im doing wrong here ?

Upvotes: 1

Views: 1231

Answers (1)

buster
buster

Reputation: 1048

With the code you've provided, it looks like only a link with the text "dummy_link" will get the cursor that you are choosing. The QTextBrowser class should automatically change the cursor if you set the proper flags.

QTextBrowser::setOpenLinks(true);

If your TextBrowserDelegate inherits from QTextBrowser you can use the following code in your constructor:

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
}

Upvotes: 0

Related Questions