PDA

View Full Version : QTextEdit on a QGraphicsWidget



paolom
7th October 2009, 11:21
Hi...

I've get the example on documentation to put a QTextEdit on a QGraphicsWidget in a QGraphicsScene.

This is the code:



textItem=new QTextEdit;
textItem->setReadOnly(true);
textItem->setFont(myFont);
textItem->setTextColor(Qt::black);
textItem->setTextBackgroundColor(Qt::lightGray);
textItem->setLineWrapMode(QTextEdit::NoWrap);
QGraphicsWidget *textEdit = addWidget(textItem);
textEdit->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Mi nimum,QSizePolicy::DefaultType);
QGraphicsGridLayout *layout = new QGraphicsGridLayout;
layout->addItem(textEdit,0,0);
layout->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Mi nimum,QSizePolicy::DefaultType);
textWidget = new QGraphicsWidget;
textWidget->setLayout(layout);
textWidget->setZValue(10);
textWidget->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Mi nimum,QSizePolicy::DefaultType);
addItem(textWidget);


where textEdit is a QTextEdit* and textWidget is a QTextWidget*.

After I set the text on the textItem with setPlainText(QString).

It works correctly but the problem is that when I show on the scene (the class where this method runs) the widget is larger then the QTextEdit, as shown in the picture.

http://www.agritur-aso.it/immagini/widget.png

How can I, in the correct way, make the QGraphicsWidget with the same size of the QTextEdit?

Thanks

axeljaeger
7th October 2009, 14:18
I think this is a feature of QTextEdit to have some margin around the text. You can try some properties of QTextEdit in QtDesigner to see whether you can make this margin disappear.

paolom
7th October 2009, 15:08
I can't obtain my goals with QTextEdit...

Adding this code:



QFontMetrics fontMetrics(myFont);
textItem->setFixedHeight(fontMetrics.height() + textItem->frameWidth() * 2);

I obtain:

http://www.agritur-aso.it/immagini/widg5.png

But I want that the width has the lenght of the QTextItem and which I can change the background color of the QGraphicsWidget...

Is it possible to make it?!

Thanks