Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2008
QTextEdit ExtraSelection - select entire line?


Message 1 in thread

Hiya - I'm trying to select the entire line within a QTextEdit using  
the ExtraSelection mechanism, but I can't figure out how to position  
the cursor at the end of the line.  It only ever seems to select the  
end of the text on a given line, but I'd like to be able to highlight  
an entire line even when it's empty.  I've tried using pretty much all  
the flags for QTextCursor to no avail.

Is this the right way to do this?  Any other suggestions?  Code I'm  
using now is below - thanks.

Liam

QTextEdit::ExtraSelection highlight;
highlight.cursor = editor->textCursor();
highlight.cursor.movePosition( QTextCursor::StartOfLine );
highlight.cursor.movePosition( QTextCursor::EndOfLine,  
QTextCursor::KeepAnchor );
highlight.format.setBackground( Qt::green );

QList<QTextEdit::ExtraSelection> extras;
extras << highlight;
editor->setExtraSelections( extras );

Message 2 in thread

Liam Staskawicz wrote:
> Hiya - I'm trying to select the entire line within a QTextEdit using 
> the ExtraSelection mechanism, but I can't figure out how to position 
> the cursor at the end of the line.  It only ever seems to select the 
> end of the text on a given line, but I'd like to be able to highlight 
> an entire line even when it's empty.  I've tried using pretty much all 
> the flags for QTextCursor to no avail.
>
> Is this the right way to do this?  Any other suggestions?  Code I'm 
> using now is below - thanks.
>
> Liam
>
> QTextEdit::ExtraSelection highlight;
> highlight.cursor = editor->textCursor();
> highlight.cursor.movePosition( QTextCursor::StartOfLine );
> highlight.cursor.movePosition( QTextCursor::EndOfLine, 
> QTextCursor::KeepAnchor );
> highlight.format.setBackground( Qt::green );
Try it with
  highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
the movePosition calls are not necessary (iirc).

Kai Koehne

--
 [ signature omitted ] 

Message 3 in thread

Works like a charm - thanks!

Liam

On Apr 14, 2008, at 12:00 AM, Kai Koehne wrote:

> Liam Staskawicz wrote:
>> Hiya - I'm trying to select the entire line within a QTextEdit  
>> using the ExtraSelection mechanism, but I can't figure out how to  
>> position the cursor at the end of the line.  It only ever seems to  
>> select the end of the text on a given line, but I'd like to be able  
>> to highlight an entire line even when it's empty.  I've tried using  
>> pretty much all the flags for QTextCursor to no avail.
>>
>> Is this the right way to do this?  Any other suggestions?  Code I'm  
>> using now is below - thanks.
>>
>> Liam
>>
>> QTextEdit::ExtraSelection highlight;
>> highlight.cursor = editor->textCursor();
>> highlight.cursor.movePosition( QTextCursor::StartOfLine );
>> highlight.cursor.movePosition( QTextCursor::EndOfLine,  
>> QTextCursor::KeepAnchor );
>> highlight.format.setBackground( Qt::green );
> Try it with
> highlight.format.setProperty(QTextFormat::FullWidthSelection, true);
> the movePosition calls are not necessary (iirc).
>
> Kai Koehne

--
 [ signature omitted ]