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

  • SEARCH
  • Home
  • 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 seems like a very simple question, but I think hours of staring at
I'm hoping this question has a very simple answer. I can think of ways
I think this is a very simple question, but I’m new to programming so
This seems like a very simple question, but I've only found complicated answers. I've
I think this may be a very simple question, but I am only just
I think that's a very simple question, but I can't deal with it: I
I think this is a very simple question. but unfortunately I cannot find a
This might be a very simple question, but I can't get it working. All
I think this has to be a very simple question but I am not
I am sure there is a very simple answer to this question, I think

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.