Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Detect mouseReleaseEvent on QImage insert in QTextedit
Forum Updated to NodeBB v4.3 + New Features

Detect mouseReleaseEvent on QImage insert in QTextedit

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pablik2005
    wrote on last edited by
    #1

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

    // .H
    #ifndef FILEE_H
    #define FILEE_H

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

    class Filee : public QWidget, public QImage
    {
    Q_OBJECT

    public:
    Filee();

    signals:

    public slots:
    void lol();

    protected :
    bool event(QEvent *);

    };

    #endif // FILEE_H

    //.CPP
    #include "filee.h"

    Filee::Filee()
    : 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");
    [/code]

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

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      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)
      {
      QTextEdit::mouseReleaseEvent(event);

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

      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Pablik2005
        wrote on last edited by
        #3

        Thx for help

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved