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. QTextEdit how one can insert Image from clipboard?
Forum Updated to NodeBB v4.3 + New Features

QTextEdit how one can insert Image from clipboard?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.0k Views 2 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.
  • B Offline
    B Offline
    bogdann
    wrote on last edited by bogdann
    #1

    HI, i try to use QMimeData, but the method

    hasImage();
    

    returns false.

    void MyTextEditor::keyPressEvent(QKeyEvent *event)
    {
        if ( event->key() == Qt::Key_V && event->modifiers() & Qt::CTRL ){
            QClipboard* clip = QApplication::clipboard();
    
            const QMimeData* mime = clip->mimeData();
            if ( mime->hasImage() )
                qDebug() << "LP";
        }
    
    
        QTextEdit::keyPressEvent( event );
    
    }
    
    K 1 Reply Last reply
    0
    • B bogdann

      HI, i try to use QMimeData, but the method

      hasImage();
      

      returns false.

      void MyTextEditor::keyPressEvent(QKeyEvent *event)
      {
          if ( event->key() == Qt::Key_V && event->modifiers() & Qt::CTRL ){
              QClipboard* clip = QApplication::clipboard();
      
              const QMimeData* mime = clip->mimeData();
              if ( mime->hasImage() )
                  qDebug() << "LP";
          }
      
      
          QTextEdit::keyPressEvent( event );
      
      }
      
      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @bogdann

      You can store the image somewhere and use img tag from html.
      That is working. However, it requires hmtl usage.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      3
      • B Offline
        B Offline
        bogdann
        wrote on last edited by
        #3

        I did it this way

        void MyTextEditor::keyPressEvent(QKeyEvent *event)
        {
            if ( event->key() == Qt::Key_V && event->modifiers() & Qt::CTRL ){
                QClipboard* clip = QApplication::clipboard();
        
                const QMimeData* mime = clip->mimeData();
        
                if ( mime->hasUrls() ) {
                    QRegExp reg(".*\\.(png|jpg|xpm)$");
                    reg.setCaseSensitivity(Qt::CaseInsensitive);
        
                    QList<QUrl> urls = mime->urls();
                    for ( int i = 0; i < urls.size(); ++i ) {
                        QString path = urls[ i ].toLocalFile();
        
                        if ( !path.contains( reg ) )
                            continue;
        
                        QImage image = QImage( path );
                        this->document()->addResource( QTextDocument::ImageResource, QUrl( path ), image );
                        this->textCursor().insertImage( path );
                    }
                    return;
                }
            }
        
        
            QTextEdit::keyPressEvent( event );
        
        }
        
        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