简体   繁体   English

"将 QPainter 与 QCoreApplication 一起使用"

[英]Using QPainter with QCoreApplication

We have an application (QCoreApplication) that takes some images as input, does something to them, and exports them again.我们有一个应用程序(QCoreApplication),它将一些图像作为输入,对它们做一些事情,然后再次导出它们。 We now need to add some text to the images, and tried to do this with the QPainter class.我们现在需要向图像添加一些文本,并尝试使用 QPainter 类来完成。 It all worked well when using it in one of our other apps (using QApplication), but not in our main QCoreApplication app.在我们的其他应用程序之一(使用 QApplication)中使用它时一切正常,但在我们的主 QCoreApplication 应用程序中却没有。

Here is the code:这是代码:

void drawTextOnImage(QImage* image, const QString& text, const QFont& font)
{
    QPainter p;
    if (!p.begin(image)) return;

    p.setFont(font);
    p.drawText(image->rect(), Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text);

    p.end();
}

When using classes from "Qt Gui" like QPainter , you are supposed to use a QGuiApplication , not a QCoreApplication . 当使用QPainter类的“ Qt Gui”中的类时,应该使用QGuiApplication而不是QCoreApplication

You might get lucky and be able to make some GUI stuff works while using only a QCoreApplication . 您可能会很幸运,并且仅使用QCoreApplication就能使某些GUI东西起作用。 But as you have discovered, it makes your application very brittle. 但是,正如您所发现的,它使您的应用程序非常脆弱。 Some classes like QPixmap will print an error message, but others will just crash. 一些类(例如QPixmap将显示错误消息,但其他类将崩溃。

The same is applicable with "Qt Widget": if you use a widget related class, you must use a QApplication . “ Qt Widget”也适用:如果使用与小部件相关的类,则必须使用QApplication

Note that since QApplication inherits QGuiApplication , if you have a QApplication you can use "Qt Gui". 请注意,由于QApplication继承了QGuiApplication ,如果您有QApplication ,则可以使用“ Qt Gui”。

In case you need to run a non-GUI application on something without a windowing system, you need, aside from creating an instance of QGuiApplication<\/code> , to also choose an appropriate Qt platform.如果您需要在没有窗口系统的情况下运行非 GUI 应用程序,除了创建QGuiApplication<\/code>实例之外,您还需要选择合适的 Qt 平台。

For me, offscreen<\/code> platform worked fine.对我来说, offscreen<\/code>平台运行良好。 I was generating images with textual elements and saving them to files on a headless Raspberry Pi.我正在生成带有文本元素的图像并将它们保存到无头 Raspberry Pi 上的文件中。 My code then was like the example below.我的代码就像下面的例子。 Note that setenv<\/code> is a POSIX function and may need a replacement on Windows, though I'm not sure whether windowless Windows is a thing at all.请注意, setenv<\/code>是一个 POSIX 函数,可能需要在 Windows 上替换,但我不确定无窗口 Windows 是否是一回事。

#include <stdlib.h>
#include <QImage>
#include <QPainter>
#include <QGuiApplication>

int main(int argc, char** argv)
{
    setenv("QT_QPA_PLATFORM","offscreen",1);
    QGuiApplication app(argc,argv);

    QImage img(128,128, QImage::Format_RGB888);
    img.fill(Qt::white);
    QPainter p(&img);
    p.drawText(QPoint(0,64), "Works!");
    img.save("/tmp/test.png");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM