Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Several ways of placing an image in a QTextEdit

I think this is a very simple question, but when I copy an image I can't paste it in a QTextEdit? Paste is inactive! Also I would like to know how to drag-and-drop a picture.

BTW I use the following code in order to insert a picture into a QTextEdit:

QTextEdit *textEditor = new QTextEdit(0);
QTextDocumentFragment fragment;
fragment = QTextDocumentFragment::fromHtml("<img src='C:\\aaa.jpg'>");
textEditor->textCursor().insertFragment(fragment);
textEditor->setVisible(true);

Is it recommended? How do you do this operation?

like image 899
Narek Avatar asked Jul 15 '10 10:07

Narek


People also ask

How do you insert a picture in text editor?

Add image from a website In your internet browser, locate the image. Right-click on it and click on Copy image. Paste it into the text editor. For more information on editing the image, refer to the Edit image section.

How do I get text from QTextEdit?

QTextEdit does not have any text() method, if you want to get the text you must use toPlainText() , if you want to clean the text it is better to use clear() since it makes it more readable.

How do you use text browser QT?

If you want to provide your users with an editable rich text editor, use QTextEdit. If you want a text browser without hypertext navigation use QTextEdit, and use QTextEdit::setReadOnly() to disable editing. If you just need to display a small piece of rich text use QLabel.


1 Answers

The second way is this:

void TextEdit::insertImage()
{
    QString file = QFileDialog::getOpenFileName(this, tr("Select an image"),
                                  ".", tr("Bitmap Files (*.bmp)\n"
                                    "JPEG (*.jpg *jpeg)\n"
                                    "GIF (*.gif)\n"
                                    "PNG (*.png)\n"));
    QUrl Uri ( QString ( "file://%1" ).arg ( 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);
 }

The third way is to inherit QTextEdit and reimplement canInsertFromMimeData and insertFromMimeData functions as follows. By the way this method allows to use drag-and-drop or copy-paste mechanisms.

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

    void insertFromMimeData(const QMimeData* source)
    {
        if (source->hasImage())
        {
            static int i = 1;
            QUrl url(QString("dropped_image_%1").arg(i++));
            dropImage(url, qvariant_cast<QImage>(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)
    {
        QFile file(url.toLocalFile());
        if (file.open(QIODevice::ReadOnly | QIODevice::Text))
            textCursor().insertText(file.readAll());
    }
};

Just wanted to share what I have found during long investigation :).

like image 187
Narek Avatar answered Oct 17 '22 13:10

Narek