Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Predicting Text Clipping with an Internationalized Application
Qt 6.12 LTS is out! See what's new in the release blog

Predicting Text Clipping with an Internationalized Application

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 736 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved