【问题标题】:Converting Text to Image (like PNG or JPEG)将文本转换为图像(如 PNG 或 JPEG)
【发布时间】:2019-07-28 16:06:02
【问题描述】:

在不使用“QApplication app(argc, argv);”的情况下将静态文本写入图像文件时需要帮助在主()。我的 QT 版本是 5.12.2。

目前我可以用下面的代码做到这一点:

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QCoreApplication::addLibraryPath("./plugins");
QString imagePath(QStringLiteral("./images/output/testOutput1.png"));
QImage image(500, 400, QImage::Format_RGB888);

image.fill(Qt::red);

QPainter painter;

if (!painter.begin(&image))
    return;

painter.setPen(QPen(Qt::black));
painter.setFont(QFont("Times", 12, QFont::Bold));
painter.drawText(image.rect(), Qt::AlignCenter, "writing text on image");
painter.end();

image.save(imagePath);

return 0;
}

我正在尝试获得一个不使用“QApplication app(argc, argv);”的类似类型的解决方案在我的代码中。

目前如果我删除“QApplication app(argc, argv);”的减速我的代码在“painter.drawText(...);”处崩溃。

【问题讨论】:

    标签: qt qt5


    【解决方案1】:

    您最多可以将QApplication 替换为QGuiApplication(从而避免来自QtWidgets 模块的无用依赖)。

    Qt 在 QCoreApplication(用于 QtCore 的东西)/QGuiApplication(用于 QtGui 的东西)/QApplication(用于 QtWidgets 的东西)的内部初始化其大部分静态/全局内容;每个都派生自另一个,所以如果你有一个QApplication,你已经拥有了 Gui 和 Core 的所有东西。

    字体引擎(和其他与绘画相关的东西)在 QGuiApplication 构造函数 as hinted in QFont documentation 中初始化

    请注意,QGuiApplication 实例必须存在才能使用QFont

    所以你不会在没有实例化它(或从它派生的类)的情况下进行绘画。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 2022-08-22
      • 2019-07-20
      • 2015-10-18
      • 2017-07-02
      • 2019-10-27
      • 1970-01-01
      相关资源
      最近更新 更多