PDA

View Full Version : attaching user data pointer to images in QTextEdit



Workeml
7th November 2008, 20:06
Hi,

I'd like to create a QTextEdit subclass in which image icons with some associated data can be included within the text.

To add an image icon and associate data, I'm using QTextFormat::setProperty():



QTextImageFormat textImage;
textImage.setName(myImageName);
QVariant v = qVariantFromValue((void *) myUserDataPtr);
textImage.setProperty(myUserDataPropertyId, v);
QTextCursor tc = textCursor();
tc.insertImage(textImage);


To retrieve the data for an image icon in the QTextEdit:



QTextFormat format = textCursor.charFormat();
if (format.isImageFormat())
{
if (format.hasProperty(myUserDataPropertyId))
{
QVariant v = format.property(myUserDataPropertyId);
// ...
}
}


This works as long as the image icons are not displaced. If I select a section of text and icons, and drag-and-drop them to a different position within the QTextEdit, the user property seems to get lost.

Is this the expected behaviour or am I doing something wrong? Should I be using a different approach than QTextFormat::setProperty()?