【发布时间】:2017-01-27 23:21:57
【问题描述】:
我必须为没有自己的图像/图片的文章创建一些图像作为占位符。我已经在 python 中实现了这一点,但我想再次在 qt 应用程序中编写代码。 python 代码正在运行,它看起来像这样:
def createImage(text="Leer", pfad="", bildname="TextBild.jpg", colorname='red'):
offset = 20 # weisses Textfeld um diesen Offset grösser darstellen
bild = Image.new('RGB', (width, height), colorname)
draw = ImageDraw.Draw(bild)
font = ImageFont.truetype("cour.ttf", 20)
w,h = draw.textsize(text, font)
draw.rectangle((((width-w-offset)/2,(height-h-offset)/2),((width+w+offset)/2,(height+h+offset)/2)), fill='white')
draw.text(((width-w)/2,(height-h)/2),text,'black',font)
## print "Erzeuge: " + pfad + os.sep + bildname, "\nPfad:\t"+pfad, "\nBildname:\t"+bildname
bild.save(pfad + os.sep + bildname) if os.path.exists(pfad) else bild.save(bildname)
但是如何在 Qt 中做到这一点?我知道有 QImage、QPainter 等,但我没有找到有用的示例。也许我做了一个系统调用来创建一个带有 qt 应用程序文本的图像。
图像如下所示:
提前感谢每一个有用的提示。
【问题讨论】:
-
QPainter用于在屏幕上绘图,而不是创建用于保存的图像。PIL/pillow更有用。 -
@furas - 这在很大程度上是不正确的。
QPainter用于在绘图设备上绘图,这包括小部件或您将其“放在屏幕上”和QImage等。 -
也许,我问错了问题。上面的 python 代码确实是它应该做的,它创建了一个包含文本的图像并将图像保存为文件。我想知道的是,如何使用 qt 和 c++ 来完成。我在 c++ 应用程序中有相同的要求,但我不想对 python 函数进行系统调用。但是我没有任何线索可以用 c++ 和 qt 类来做到这一点。那么,也许 c++ 开发人员的某个人可以帮助我提供 c++ 端的等效源代码?