How to save and read image in QTextEdit



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.