Detect mouseReleaseEvent on QImage insert in QTextedit

  • Hi i want detect when i click on Qimage inserted to QTextEdit . i make Filee class but dont work.

    // .H
    #ifndef FILEE_H
    #define FILEE_H

    #include <QWidget>
    #include <QMessageBox>
    #include <QEvent>

    class Filee : public QWidget, public QImage



    public slots:
    void lol();

    protected :
    bool event(QEvent *);


    #endif // FILEE_H

    #include "filee.h"

    : QImage(":/File.png")

    bool Filee::event(QEvent * e)
    QMessageBox::information(0, QString::number(e->type()) , "sdf");
    return true;

    //Add Filee to textedit
    Filee f1;
    ui->textEdit->textCursor().insertImage( (QImage) f1, "f1");

    After click on this image in textedit do nothing , event are not detected ;(

  • Moderators

    QEvents are only propagated to QObjects/QWidgets. Since QImage doesn't derive from it (and you shouldn't add it either) it won't receive events.

    Instead listen to the mouse event of QTextEdit and check if it's an image on that position:
    void MyTextEdit::mouseReleaseEvent(QMouseEvent * event)

    int pos = this->document()->documentLayout()->hitTest(event->pos(), Qt::ExactHit);
    if(pos > 0)
        QTextCursor cursor(document());
        if( ! cursor.atEnd() )
            QTextFormat format = cursor.charFormat();
            if( format.isImageFormat() )
                QString image = format.toImageFormat().name();
                //... do whatever you want


  • Thx for help

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.