2

I have to create some Images as placeholder for articles which have no own images / pictures. I already realized this in python, but i want to write the code in a qt app again. The python code is working and it looks like this:

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)

But how to do this in Qt? I know there is QImage, QPainter, etc. but I dont't find a example what ist useful. Perhaps I do a system call to create a Image with text out of a qt app.

The image looks like this:

enter image description here

Thanks in advance for every useful hint.

furas
  • 134,197
  • 12
  • 106
  • 148
Teddy2000
  • 21
  • 1
  • 2
  • `QPainter` is for drawing on screen, rather not to create images for save. `PIL/pillow` is more usefull. – furas Jan 27 '17 at 23:56
  • See [QImage::save()](http://doc.qt.io/qt-5/qimage.html#save) – MrEricSir Jan 28 '17 at 01:02
  • 1
    @furas - this is largely incorrect. `QPainter` is for drawing on paint devices, this includes widgets or as you put it "on screen" and `QImage` among others. – dtech Jan 28 '17 at 02:41
  • Perhaps, I ask the wrong question. The python code above does excatly was it should do, it creates a image with text in it and save the image as a file. What I wanted to know, how this can be done with qt and c++. I have the same requirement in a c++ app, but I don't want to make a system call to a python function. But I don't have any clue to do this with c++ an qt classes. So, perhaps someone from the c++ developers could help me with an equivalent source code on the c++ side? – Teddy2000 Jan 28 '17 at 21:07

2 Answers2

3

For this particular image that you've given this will do the job:

QImage image(QSize(400,300),QImage::Format_RGB32);
QPainter painter(&image);
painter.setBrush(QBrush(Qt::green));
painter.fillRect(QRectF(0,0,400,300),Qt::green);
painter.fillRect(QRectF(100,100,200,100),Qt::white);
painter.setPen(QPen(Qt::black));
painter.drawText(QRect(100,100,200,100),"Text you want to draw...");
image.save("testImage.png");
mostafaTmj
  • 363
  • 4
  • 12
0

Perfect! Thats excatly what I was looking for. Thank you very much!

bool createImage(QString text="Leer", QString path="./", QString imageName="TextImage.png", QColor aColor=Qt::red)
{
    int width = 1024;
    int height = 768;
    int offset = 25;
    int w = 400;
    int h = 200;

    QImage image(QSize(width,height),QImage::Format_RGB32);
    QPainter painter(&image);
    painter.setBrush(QBrush(aColor));
    painter.fillRect(QRectF(0,0,width,height),Qt::darkGreen);
    qDebug() << (width-w-offset)/2 << "\t" << (height-h-offset)/2 << "\t" << w << "\t" << h;
    QRect aRect = QRect( (width-w)/2, (height-h)/2, w, h );
    QRect aRectOffset = QRect( (width-w+offset)/2, (height-h+offset)/2, w-(offset/2), h-(offset/2) );
    painter.fillRect(QRect(aRect),Qt::white);
    painter.setPen(QPen(Qt::black));
    painter.setFont(QFont( "Courier", 20) );
    painter.drawText(QRect(aRectOffset),text);
    QDir aDir = QDir(path);
    if ( aDir.mkpath(path) )
        return image.save(path + "/" + imageName);
    else
        return image.save(imageName);
}

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

    createImage("Ein einfacher Text\nDer auch mal länger sein kann.", "", "TestBild.png", Qt::green);

    return a.exec();
} 

I just played a little bit with your code above and now it is working for me really good. :-)

Teddy2000
  • 21
  • 1
  • 2