Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QTextEdit how one can insert Image from clipboard?

    General and Desktop
    2
    3
    1593
    Loading More Posts
    • 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
      bogdann last edited by 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 1 Reply Last reply Reply Quote 0
      • K
        koahnig @bogdann last edited by

        @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 Reply Quote 3
        • B
          bogdann last edited by

          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 Reply Quote 0
          • First post
            Last post