PDA

View Full Version : QTextEdit: how to get image for given position in the viewport ?



Vladimir
2nd October 2007, 19:39
I have QTextEdit with richtext and embedded images in it. In mousePressEvent I need to know which image is under cursor. Are there any way to achieve it ?

I've tried cursorForPosition(e->pos()).charFormat() but it gives the image which will be just before text cursor if I'll click at the e->pos() position, which is not what I want. I want behavior like anchorAt() but for images.

Vladimir
3rd October 2007, 07:34
I've found the solution:



int pos = document()->documentLayout()->hitTest(e->pos(), Qt::ExactHit);
if(pos < 0) break;

QTextCursor cursor(document());
cursor.setPosition(pos);
if(cursor.atEnd()) break;
cursor.setPosition(pos+1);

QTextFormat format = cursor.charFormat();
if(!format.isImageFormat()) break;
QString image = format.toImageFormat().name();
kDebug() << image << endl;