I would like to launch URLs from a QTextEdit widget. The URLs are custom styled. I have made a naive attempt:
Writing text to the QTextEdit:
bool Text::WriteText( const QStringRef& str, bool inurl ) // Text is "public QTextEdit", inurl is true if we are appending an URL
{
if( !str.isEmpty() )
{
if( inurl )
{
cf.setAnchorHref(str.toString());
mergeCurrentCharFormat(cf);
}
insertPlainText(str.toString());
}
return true;
}
bool Text::WriteText( const QStringRef& str, bool inurl ) // Text is "public QTextEdit", inurl is true if we are appending an URL
{
if( !str.isEmpty() )
{
if( inurl )
{
QTextCharFormat cf(currentCharFormat());
cf.setAnchorHref(str.toString());
mergeCurrentCharFormat(cf);
}
insertPlainText(str.toString());
}
return true;
}
To copy to clipboard, switch view to plain text mode
mouse click (debugging version for now, launch iceweasel when it will work):
{
bool bb;
if( event->buttons() == Qt::LeftButton )
{
bb = url.isEmpty();
}
}
void Text::mousePressEvent( QMouseEvent *event )
{
bool bb;
if( event->buttons() == Qt::LeftButton )
{
QTextCursor cu(cursorForPosition(event->pos()));
QTextCharFormat cf(cu.charFormat());
QString url(cf.anchorHref());
bb = url.isEmpty();
QTextEdit::mousePressEvent(event);
}
}
To copy to clipboard, switch view to plain text mode
Works ... almost. If I click a URL in the text window, the "url" string contains the URL. If I click another URL, I get another URL correctly. But if I click a non-URL text, I get the last URL clicked. Not acceptable. What am I doing wrong?
Bookmarks