Search code examples
c++qtqlineedit

Calculate max length of QLineEdit to suit its width


I created some QLineEdit, which have different sizes.

I want to set the max length of those to suit the width of them.

To be more specific: for example, with a width of 50, I will only allow to enter about 7 characters, because the size of each character is different.

How can I exactly calculate the maximum length needed?


Solution

  • You can set a limit based on the width of myLineEdit such that it will be able to fit all characters up to that limit by doing this:

    #include <QFontMetrics>
    #include <QLineEdit>
    
    QLineEdit myLineEdit;
    
    // get max character width of the line edit's font
    int maxWidth = QFontMetrics(myLineEdit->font()).maxWidth();
    
    // find the character limit based on the max width
    int charlimit = myLineEdit.width() / maxWidth;
    
    // set the character limit on the line edit
    myLineEdit->setMaxLength(charlimit);
    

    However, here are some reasons you probably don't want to in production code:

    1. Stylesheets - what happenes when your designer gives the edit a 14px border?
    2. Resizing - you'll have to adjust this on every resize, and I've found that in practice it's a very difficult thing to keep track of. It's not a useful feature if it breaks whenever the GUI gets resized.
    3. Content - the width of the line edit, especially one that a user can enter text into, is not logically related to the length of text the user may need to enter. This could become an arbitrary constraint that frustrates your users. No one likes horizontal scrolling, but sometimes there are use cases.