Predicting Text Clipping with an Internationalized Application



  • I am working on an application which has been translated into 15+ languages. We wrote this generic code to traverse through the widget hierarchy and calculate text width and compare it with the content width of the widget. QLabel is giving us many false positives and false negatives. Has anyone else done this? What might be wrong with the following code? It fails in about 30% of the time.

    @
    QList<QWidget *> childWidgets = parent->findChildren<QWidget >();
    foreach (QWidget
    w, childWidgets)
    {
    QFontMetrics fm = w->fontMetrics();

        int textWidth=0, contentWidth=0;
    
        QLabel* label = qobject_cast<QLabel*>(w);
        if (label)
        {
            // measure any text for a label
            QString txt = label->text();
            textWidth = fm.width(txt);
            contentWidth = label->contentsRect().width();
            if (label->wordWrap())
            {
                int lines = label->contentsRect().height() / fm.height();
                if (lines > 1)
                    contentWidth *= lines;
            }
        }
    

    ....
    // alter the style for any widget we measured the text
    if (textWidth)
    {
    bool textTooBig = textWidth > contentWidth;
    setTextExtentsProblem(w, textTooBig);
    }
    }
    @

    In most of the cases the textWidth is larger than the contentWidth despite the fact that it appears to fit upon visual inspection.

    Thoughts?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.