【问题标题】:QTextDocument::DrawContents skips resources?QTextDocument::DrawContents 跳过资源?
【发布时间】:2018-01-24 14:13:25
【问题描述】:

我有这个设置:

// ...

// variable document is a QTextDocument* which has some 'RichText' + 'Images'

QTextEdit textEdit;

textEdit.setDocument(document);

textEdit.setLineWrapMode(QTextEdit::LineWrapMode::NoWrap);

auto image = QImage(document->size().width(), document->size().height(),
                       QImage::Format_ARGB32_Premultiplied);

image.fill(Qt::transparent);

QPainter painter(&image);

document->drawContents(&painter);
     
// ...

我这样做是为了让我的文本在一个长的水平 QImage 中呈现(因此是“NoWrap”LineWrapMode),所以我可以使用 QImage::copy(QRect) 一次选择它的一小部分并创建一个滚动文字效果。

我这样做的原因是我需要在最后有一个 QImage,然后我会将其缓冲区 (QImage::bits()) 提供给我用作最终输出的硬件.

所以它工作得很好,它显示带有字体和颜色的格式化文本以及除图像之外的所有内容,它似乎跳过它们,注意“带有图像的文本结果”图片中的文件图标。

这只是编辑器中的文本

这只是文本的结果

这是编辑器中带有图像的文本

这是文字与图片的结果

这就是我向 QTextDocument 插入图像的方式:

QImage image(url.toLocalFile());

if (image.isNull())
    return;

image = image.scaledToHeight(getDocumentHeight(), Qt::SmoothTransformation);

auto filename = QUrl(url.fileName());

textEdit->document()->addResource(QTextDocument::ImageResource, filename, image);

textEdit->textCursor().insertImage(filename);

所以我认为不是因为“DrawContents”找不到图像资源文件或类似的东西。

我该怎么办?有什么我想念的吗?非常感谢您对此事的任何帮助! ;)

【问题讨论】:

  • @eyllanesc 它们属于两个不同的类别,因为它们是一个更大项目的一部分。无论如何,就您而言,它似乎运行良好,我不知道为什么,您做了什么?我在 ubuntu linux 上。

标签: c++ qt qt5 qtextedit


【解决方案1】:

在下面的代码中,我展示了如何加载图像,然后将其保存到文件中,可能错误是您尚未完成绘画,为此您必须调用painter.end()或从内存中删除画家。

ma​​in.cpp

#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget widget;
    QVBoxLayout vlayout(&widget);
    QTextEdit textEdit;
    QPushButton button("save image");
    QPushButton loadButton("Load and Insert");
    vlayout.addWidget(&loadButton);
    vlayout.addWidget(&textEdit);
    vlayout.addWidget(&button);

    widget.show();

    textEdit.append("some text");

    QObject::connect(&loadButton, &QPushButton::clicked,[&textEdit](){
        QString filename =  QFileDialog::getOpenFileName(&textEdit, "Select", "", "*.png");
        if(!filename.isEmpty()){
            QImage image(filename);
            QUrl url = QUrl::fromLocalFile(filename);
            image = image.scaledToHeight(100, Qt::SmoothTransformation);
            textEdit.document()->addResource(QTextDocument::ImageResource, url, image);
            textEdit.textCursor().insertImage(image);
        }
    });

    QObject::connect(&button, &QPushButton::clicked, [&textEdit](){
        QImage image(textEdit.document()->size().toSize() , QImage::Format_ARGB32_Premultiplied);
        image.fill(Qt::transparent);
        QPainter painter(&image);
        textEdit.document()->drawContents(&painter);
        painter.end();
        image.save("image.png");

    });

    return a.exec();
}

【讨论】:

  • :) 效果很好!
  • @eyllanesc 这很好用!我标记了你的答案,谢谢你的努力。我认为画家的析构函数调用了结束,在我使用图像之前它确实超出了范围,所以情况可能并非如此,而且你没有使用 setLineWrapMode 所以也许它与它有关,现在是深夜现在,但我明天会尝试根据你的代码修复我的代码,我会更新帖子。再次感谢你。 :)
猜你喜欢
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 2019-07-26
  • 1970-01-01
  • 2012-03-18
相关资源
最近更新 更多