QTextEdit: how to get image for given position in the viewport ?
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.
Re: QTextEdit: how to get image for given position in the viewport ?
I've found the solution:
Code:
int pos = document()->documentLayout()->hitTest(e->pos(), Qt::ExactHit);
if(pos < 0) break;
cursor.setPosition(pos);
if(cursor.atEnd()) break;
cursor.setPosition(pos+1);
if(!format.isImageFormat()) break;
QString image
= format.
toImageFormat().
name();
kDebug() << image << endl;