Solved QTextEdit how one can insert Image from clipboard?
-
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 ); }
-
You can store the image somewhere and use img tag from html.
That is working. However, it requires hmtl usage. -
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 ); }