PDA

View Full Version : protect some text in a QTextEdit



GrahamLabdon
10th October 2011, 13:28
Hi
Is there a way of making some text within a QTextEdit non editable?
I want to insert some text programatically but prevent the user from editing this text.

thanks

wysota
10th October 2011, 13:31
I think you'd need to implement your own QTextObject subclass.

GrahamLabdon
10th October 2011, 14:03
Thanks for the hint
How would I make my QTextObject read only?

wysota
10th October 2011, 14:06
You can start by reading the docs, especially the Text Object Example.

GrahamLabdon
10th October 2011, 15:48
Hi
OK, I have read the relevant docs and started to implement my own text object for inserting a string.
I can now get this displayed, but am struggling with how to correctly implement the 'intrinsicSize' method.

inside this method I can obtain the string to be inserted and then i tried to calculate the size required to display it


QString str = qVariantValue<QString>(format.property(Window::ProtectedTextData));
QTextBlock block = doc->findBlock(posInDocument);
QTextCharFormat fmt = block.charFormat();

qreal pointSize = fmt.fontPointSize();
size.setHeight(pointSize);
size.setWidth(pointSize*str.length());

but he size is always 0.
I would appreciate any hint as to how to correctly implement this

Thanks

wysota
10th October 2011, 16:21
QFontMetrics::boundingRect()

GrahamLabdon
10th October 2011, 17:01
Hi
Thanks for the help

Inside the 'intrinsicSize' method I retrive the font by -

QTextBlock block = doc->findBlock(posInDocument);
QTextCharFormat fmt = block.charFormat();
QFont font = fmt.font();
QFontMetrics fm(font);
QRect rect = fm.boundingRect(str);

But this always returns the same thing, I want the font being used in the document at the point that I insert the text.
Why does my code not work?

Thanks

wysota
10th October 2011, 17:06
This returns the text format of the whole block. If you want the format for your item, use QTextObject::format() and convert it to the proper type. You can also use QTextCursor::charFormat().

GrahamLabdon
11th October 2011, 08:53
Hi
Within the intrinsicSize method I cannot get on object of type QTextObject or QTextCursor.


QTextObject *textObject = doc->objectForFormat(format);
returns a null pointer.
QTextCursor belongs to the QTextEdit class which does not seem to be available in this method

Am I missing something?

Graham

wysota
11th October 2011, 08:55
QTextCursor doesn't belong to QTextEdit class. You can have as many cursors as you want, have a look at available constructors. And I have no idea what your objectForFormat() call was supposed to do. You are implementing QTextObject subclass so just calling format() will return a QTextFormat which you can then convert to another type using the API available in QTextFormat.

GrahamLabdon
11th October 2011, 09:03
OK now I am confused
You say I am implementing a QTextObject subclass, but I am just following the TextObject example that implements
QTextObjectInterface.
Where should I be creating a QTextObject ?

Thanks

wysota
11th October 2011, 09:09
True. However you get a QTextFormat as the argument to intrinsicSize(), haven't you noticed?

GrahamLabdon
11th October 2011, 10:08
I have made some progress with he following implementation

QSizeF SvgTextObject::intrinsicSize(QTextDocument * doc, int posInDocument,
const QTextFormat &format)
{
QSize size;

QString str = qVariantValue<QString>(format.property(Window::SvgData));

QTextCursor cursor(doc);
QTextCharFormat fmt = cursor.charFormat();

QFont font = fmt.font();
QFontMetrics fm(font);
QRect rect = fm.boundingRect(str);

size.setHeight(rect.height());
size.setWidth(rect.width());
return QSizeF(size);
}



void SvgTextObject::drawObject(QPainter *painter, const QRectF &rect,
QTextDocument * /*doc*/, int /*posInDocument*/,
const QTextFormat &format)
{
const QString str = qVariantValue<QString>(format.property(Window::SvgData));
QTextCursor cursor(doc);
QTextCharFormat fmt = cursor.charFormat();

QFont font = fmt.font();
painter->setFont(font);
painter->drawText(rect,str);
}


Is this the correct way or is there a better way?

Thanks for your help

wysota
11th October 2011, 13:18
If it works for you, it is fine with me.

GrahamLabdon
11th October 2011, 13:20
Thanks for your assistance
But I'm not sure that its going to help me!
I need to be able to save the document but when I do so the custom object is not saved
Need some more research
Any hints on this

Thanks

wysota
11th October 2011, 14:24
How are you "saving" it?

GrahamLabdon
11th October 2011, 14:25
I tried converting the contents to html and saving that
I also tried saving as an odt document

wysota
11th October 2011, 14:34
HTML doesn't allow any "protected text" so there is no way you are going to succeed. ODT writer only allows simple things, so no joy here too.

GrahamLabdon
11th October 2011, 14:35
Is there anything that might work?

wysota
11th October 2011, 14:38
You can implement your own writer and your own reader if you need to recreate the text document from a file.

GrahamLabdon
11th October 2011, 14:46
I thought that's what you'd say!
Thanks for all your help