Results 1 to 2 of 2

Thread: QTextEdit: how to get image for given position in the viewport ?

  1. #1
    Join Date
    Jan 2007
    Posts
    45
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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.

  2. #2
    Join Date
    Jan 2007
    Posts
    45
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTextEdit: how to get image for given position in the viewport ?

    I've found the solution:

    Qt Code:
    1. int pos = document()->documentLayout()->hitTest(e->pos(), Qt::ExactHit);
    2. if(pos < 0) break;
    3.  
    4. QTextCursor cursor(document());
    5. cursor.setPosition(pos);
    6. if(cursor.atEnd()) break;
    7. cursor.setPosition(pos+1);
    8.  
    9. QTextFormat format = cursor.charFormat();
    10. if(!format.isImageFormat()) break;
    11. QString image = format.toImageFormat().name();
    12. kDebug() << image << endl;
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Vladimir for this useful post:

    pbek (14th March 2017)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.