PDA

View Full Version : URL in QTextEdit



Radek
28th August 2013, 08:40
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 )
{
QTextCharFormat cf(currentCharFormat());

cf.setAnchorHref(str.toString());
mergeCurrentCharFormat(cf);
}

insertPlainText(str.toString());
}

return true;
}


mouse click (debugging version for now, launch iceweasel when it will work):


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


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?

wysota
3rd September 2013, 22:09
Most probably the text char format is incorrect. Do you get the same problem if you insert the data into the text edit widget with code using your own crafted text char formats?