Qt:向QTextEdit中插入图片

Qt:向QTextEdit中插入图片
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

void UiUtil::showDetails(QTextEdit *edit, const QString &content, const QString &imagePath) {

    QString text = content;

    edit->setHtml(QString("<html>%1</html>").arg(text.replace("\n", "<br>")));


    QImage image(imagePath);

    if (image.isNull()) { return; }


    int width = edit->viewport()->width() - 7;

    if (image.size().width() > width || image.size().height() > width) {

        image = image.scaled(width, width, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    }


    QTextCursor cursor = edit->textCursor();

    QTextDocument *document = edit->document();

    // 下面的addResource代码非常重要, 内存增加很少

    cursor.movePosition(QTextCursor::End);

    document->addResource(QTextDocument::ImageResource, QUrl("image"), image);

    // edit->setAlignment(Qt::AlignCenter);

    cursor.insertImage("image");

}

你可能感兴趣的:(Qt:向QTextEdit中插入图片)