PDA

View Full Version : QLabel and word rectangle



Syntho
23rd September 2009, 21:54
Hello!
I developing my own widget based on QLabel. Normally it is just display text with hyperlinks (without correct address, I'm using it only for mouseClick detection), but when user clicks on link it show 'in-place' editor over hyperlink clicked.

But now I cannot continue my work because I cannot detemine right rectangle for some words in QLabel. How can I do it?

Now I'm learning sources of QLabel/QTextLayout and QTextLine but cannot find anything suitable.

Also, text in QLabel may contains line breaks, so code like


void QLegendLabel::OnLinkActivated(QString link)
{
...
QTextLayout lt(text(), font());
lt.beginLayout();

//Replace all <br/> tags with \n and remove other HTML tags, working with text
QString textLayouted = PrepareString(text());

QTextLine line = lt.createLine();

while(line.isValid())
{
line.setLineWidth( contentsRect().width() );
QString lineText = textLayouted.mid(line.textStart(), line.textLength());

qDebug() << "Created line" << line.lineNumber()
<< "width " << line.width()
<< "textStart" << line.textStart()
<< "textLength" << line.textLength()
<< "text" << lineText;
line = lt.createLine();
}

lt.endLayout();
...
}


For text line like


const char* pcTtfLegend = "PC TrueType font stored as TTF file: "\
"<a href='edit:21'>HERE FILE NAME</a><br/>Used encoding is "\
"<a href='menu:12'>Unicode</a><br/>Font family is <a href='edit:13'>FAMILY</a> "\
"and full names is <a href='menu:14'>FULL NAME</a>";


I've got next QTextLines:


Created line 0 width 194 textStart 0 textLength 37 text "PC TrueType font stored as TTF file: "
Created line 1 width 194 textStart 37 textLength 28 text "HERE FILE NAME
Used encoding"
Created line 2 width 194 textStart 65 textLength 33 text " is Unicode
Font family is FAMILY"
Created line 3 width 194 textStart 98 textLength 36 text " and full names is FULL NAME"
Created line 4 width 194 textStart 134 textLength 13 text ""
Created line 5 width 194 textStart 147 textLength 35 text ""
Created line 6 width 194 textStart 182 textLength 32 text ""
Created line 7 width 194 textStart 214 textLength 8 text ""


All that I want is to get QRectF wich QLabel draw around selected hypelink =)