Insert an image into a QTextEdit

源赖朝源赖朝
2 min read

1. Using html tag

QTextEdit *textEditor = new QTextEdit(0);
QTextDocumentFragment fragment;
fragment = QTextDocumentFragment::fromHtml("");
textEditor->textCursor().insertFragment(fragment);
textEditor->setVisible(true);

2. Using QTextDocument's resource

QString file = QFileDialog::getOpenFileName(this, tr("Select an image"),
".", tr("Bitmap Files (*.bmp)
"
"JPEG (*.jpg *jpeg)
"
"GIF (*.gif)
"
"PNG (*.png)
"));
QUrl Uri ( file );
QImage image = QImageReader ( file ).read();
QTextDocument * textDocument = m_textEdit->document();
textDocument->addResource( QTextDocument::ImageResource, Uri, QVariant ( image ) );
QTextCursor cursor = m_textEdit->textCursor();
QTextImageFormat imageFormat;
imageFormat.setWidth( image.width() );
imageFormat.setHeight( image.height() );
imageFormat.setName( Uri.toString() );
cursor.insertImage(imageFormat);

3. Subclass QTextEdit (The example below also enabled drag & drop)

class TextEdit : public QTextEdit {
public:
    bool canInsertFromMimeData( const QMimeData* source ) const
    {
        return(source->hasImage() );
    }


    void insertFromMimeData( const QMimeData* source )
    {
        if ( source->hasImage() )
        {
            static int    i = 1;
            QUrl        url( QString( "dropped_image_%1" ).arg( i++ ) );
            dropImage( url, qvariant_cast( source->imageData() ) );
        } else if ( source->hasUrls() )
        {
            foreach( QUrl url, source->urls() )
            {
                QFileInfo info( url.toLocalFile() );
                if ( QImageReader::supportedImageFormats().contains( info.suffix().toLower().toLatin1() ) )
                {
                    dropImage( url, QImage( info.filePath() ) );
                }else  {
                    dropTextFile( url );
                }
            }
        }else  {
            QTextEdit::insertFromMimeData( source );
        }
    }
}

private:
void dropImage( const QUrl & url, const QImage & image )
{
    if ( !image.isNull() )
    {
        document()->addResource( QTextDocument::ImageResource, url, image );
        textCursor().insertImage( url.toString() );
    }
}


void dropTextFile( const QUrl & url )
{
    textCursor().insertText( file.readAll() );
}
};
0
Subscribe to my newsletter

Read articles from 源赖朝 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

源赖朝
源赖朝