Question

I've got a really simple application for adding watermarks to pictures. So you can drop your pictures in a QListWidget which shows you a thumbnail and the path, adjust some things like the text, the transparency, the output format and so on.. and after pressing start it saves the copyrighted picture in a destination of your choice. This works with a QPainter which paints the logo and text on the picture.

Everything is able to work fine. But here's the misterious bug:

The application kills random letters. It's really strange, because I can't reproduce it. With every execution and combination of options it's different. For example:

  • Sometimes I can't write some letters in the QLineEdit of my interface (like E, 4 and 0 doesnt exist, or he changes the letters so some special signs).
  • The text of the items in the QListWidget aren't completly displayed, sometimes completley missing. But I can extract the text normally and use the path.
  • While execution I have a QTextBrowser as a log to display some interesting things like the font size. Although, the font is shown normaly on the resulting picture, it says " 4" or "6" instead of much higher and correct sizes. Betimes strange white blocks appear between some letters
  • When drawing text on the picture with a QPainter, there also letters missing. Sometimes, all the letters are printed over each other. It seems like this bug appears more often when using small pixelsizes (like 12):

        //Text//
    int fontSize = (watermarkHeight-(4*frame));
    int fontX = 2*frame;
    int fontY = (result.height()-(watermarkHeight-2*frame));
    int fontWidth = watermarkWidth;
    QRect place(fontX,fontY,fontWidth,fontSize);
    
    QFont font("Helvetica Neue", QFont::Light);
    font.setPixelSize(fontSize);
    emit log(QString::number(fontSize));
    pixPaint.setFont(font);
    pixPaint.setPen(QColor(255,255,255,textOpacity));
    pixPaint.drawText(place,text);
    

Not all of these bugs appear at once! Sometimes I haven't got any bugs...

Perhaps someone had a similar bug before. Unfortunately I didn't found something like this in the internet. I didn't post a lot of code snippets because I think (and hope) that this is a gerneral problem. If you need something specific to help me, please let me know =)

I've added a example picture:

  • In the lineEdit I simply wrote ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 (look what he has done with the 7 and 9)
  • This small square in the lower corner of the picture should be the "ABC..." thing
  • The "62" looks very strange in the textBrowser

example screenshot

I'm using Qt 5.0.1 on a Windows 7 64Bit computer.

EDIT: Everytime after adding the first picture to the list, he's finding these warnings:

QFontEngine::loadEngine: GetTextMetrics failed ()
QWindowsFontEngine: GetTextMetrics failed ()

But when I change the height (and with it the pointSize of the font) its not emitted anymore, even with the start-parameters.

EDIT 2: Thank you for your help! I corrected my code so that he only uses correct fonts and correct sizes, but it still doesn't work. When I remove the QPainter::drawText() function it works fine (without the text). But as soon as I am adding text everything is bugged. I have something like this now:

    //Text//
    QList<int> smoothSizes = fontDatabase->smoothSizes("Verdana","Standard");
    int fontSize = (watermarkHeight-(4*frame))*0.75;
    emit log("Requested:  "+QString::number(fontSize));

    if(!smoothSizes.contains(fontSize)){
        for(int i = 0; i<smoothSizes.length(); i++){
            if(smoothSizes.at(i) > fontSize && i>0){
                fontSize = smoothSizes.at(i-1);
                break;
            }
        }
    }

    int fontX = 2*frame;
    int fontY = (result.height()-(watermarkHeight/2)+frame);

    QFont font = fontDatabase->font("Verdana","Standard",fontSize);

    QFontInfo info(font);
    emit log("Corrected: "+QString::number(fontSize));
    emit log("Okay?: "+QString::number(info.exactMatch()));

    pixPaint.setFont(font);
    const QFontMetrics fontMetrics = pixPaint.fontMetrics();

    if(info.exactMatch()){
        pixPaint.setPen(QColor(255,255,255,textOpacity));
        pixPaint.drawText(fontX,fontY+(fontMetrics.height()-fontMetrics.ascent()),text);
    }
Was it helpful?

Solution

It almost sounds like you are corrupting random memory in your process, or you have a badly broken Windows install. Possibly your font request is matched by a very poorly chosen system font.

Whatever is set on a QFont is merely a request. To obtain the parameters of the actual font that was selected, you must create a QFontInfo, and get your information from there.

Imagine that you request a QFont that doesn't exist on a system, or that can't be scaled to a particular size. At some point the font object would need to morph to reflect what really happened - this would be very confusing. Thus, the QFontInfo provides the information about the font that was actually used. Think of QFontInfo as a response, and QFont as a request.

OTHER TIPS

I finally found a solution: I simply updated Qt from 5.0.1 to 5.2.1, now it works. Perhaps someone has a similar bug and this post helps him. Thank you for your help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top