Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to save and read image in QTextEdit
Qt 6.12 LTS is out! See what's new in the release blog

How to save and read image in QTextEdit

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    soleverlee
    wrote on last edited by
    #1

    I want to paste or insert an image into a QTextEdit, and then save it to the database;when read the stored html string from database, the image will show.I tried to use the follow codes, now it's able to paste an image,(although sometimes it's not available to paste text).
    @void QImageTextEdit::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);
    }
    }

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

    void QImageTextEdit::dropTextFile(const QUrl& url)
    {
    QFile file(url.toLocalFile());
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    textCursor().insertText(file.readAll());
    }
    @
    But how to save the image? If I save the image to a folder, then what should I do to show it?

    By the way, I save the formats (just like the high-light, font, size, color etc) as html by QTextEdit.toHtml(), and then save it to
    sqlite3 in text, any better ideas?(I wander how word makes it)

    Thanks.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved