PDA

View Full Version : Immediate help is required on QGraphicsTextItem



Sandip
19th November 2008, 08:29
Hi,

I have customized QGraphicsTextItem, added into scene.

I want to align the text in that item.

I tried following way but unable to get results.

Please help me.

alignment - is Qt::Alignment


QTextCursor objCursor = textCursor();
objCursor.movePosition(QTextCursor::Start);
objCursor.movePosition(QTextCursor::StartOfBlock);
QTextBlockFormat txtBlockFormat = objCursor.blockFormat();
txtBlockFormat.setAlignment(alignment);

objCursor.setBlockFormat(txtBlockFormat);

//or
objCursor.mergeBlockFormat(txtBlockFormat);

setTextCursor(objCursor);

The above code is supposed to align first block only.

I am expecting the results shown in attached image file.

Is there any other way to do it?

Thanks in advance.

Regards,
Sandip

wysota
19th November 2008, 10:57
Select the first block before applying the block format.

Sandip
19th November 2008, 12:44
Select the first block before applying the block format.

What do you mean by "Select the first block"?

I tried

objCursor.select(QTextCursor::BlockUnderCursor);

is it correct?

thanks
Sandip

wysota
19th November 2008, 17:01
Yes, that's what I meant. Just make sure your text really contains more than one block or else you'll achieve the same result as before.

Sandip
20th November 2008, 07:55
Hi Wysota,

It is working after setting text width of document.

Is there any way to tell it to grow or shrink automatically?

If text width will be set to -1 then it grows and shrinks automatically but alignment doesn't works.

Problem is that bounding dotted rectangle is shown according to text width but actual text may be too small. So it looks bad in my application.

Thanks for your replies.

wysota
20th November 2008, 09:39
You have to set the width manually or come up with a heuristic to calculate it. Without the width the document doesn't know how much space it occupies so it is hard to layout it properly.

nicolas1
21st November 2008, 00:44
This helped me. It was called initially:



QTextDocument *text_document = text_item->document();

if (NULL != text_document)
{
text_document->setTextWidth(width);

QTextOption alignment_option(Qt::AlignHCenter);

text_document->setDefaultTextOption(alignment_option);
}