Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1073875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:02:49+00:00 2026-05-16T21:02:49+00:00

I think this is a very simple question, but when I copy an image

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:02:49+00:00Added an answer on May 16, 2026 at 9:02 pm

    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 :).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something that I think would be very useful. Basically, I'd like there
I think this must be simple but I can't get it right... I have
This is more then likely a very simple question but I am completely stumped
All, This question probably has a very simple answer - something I'm overlooking. But
I guess this is a quite simple (read: very simple) question, but I have
This is probably a very silly and simple question to most of you, but
I think this will be easy but I can't see how to do it!
I think this must be a stupid question, but why do the results of
I'm not very familiar with machine code, but I think this is a pretty
I'm hoping someone can help with this very newbie question. I've just come from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.