Trolltech | Documentation | Qt Quarterly | « Fancy List Items | Internationalization Q & A »

Fields on Forms
by Mark Summerfield
Forms that have lots of line edits often use a grid layout with labels in the first column and line edits in the second column. This looks attractive, except that fields that can only accept a short amount of text, say one or two characters, are given the same width as fields that can take a long line of text. This deprives users of the visual cue that proportional widths would give. This article provides a simple solution.

We need a QLineEdit subclass that lets us set the minimum and maximum number of visible characters in a line edit. This has been provided by Qt Quarterly reader Jake Colman. [1]

    class FixedLineEdit : public QLineEdit
        Q_PROPERTY(int minVisibleChars READ minVisibleChars WRITE setMinVisibleChars)
        Q_PROPERTY(int maxVisibleChars READ maxVisibleChars WRITE setMaxVisibleChars)
        FixedLineEdit(QWidget *parent, const char *name = 0)
            : QLineEdit(parent, name)
        int minVisibleChars() const { return minVisible; }
        int maxVisibleChars() const { return maxVisible; }
        void setMinVisibleChars(int count)
            minVisible = count;
            setMinimumWidth(fontMetrics().width('W') * minVisible +
			    ((style().defaultFrameWidth() + 2) * 2));
        void setMaxVisibleChars(int count)
            maxVisible = count;
            setMaximumWidth(fontMetrics().width('W') * maxVisible +
			    ((style().defaultFrameWidth() + 2) * 2));
        int minVisible;
        int maxVisible;

This class is defined entirely in the header file fixedlineedit.h. It must include both the <qlineedit.h> and <qstyle.h> header files. Jake used the letter 'W' as the basis of his calculation. This means that a FixedLineEdit with its minimum visible characters set to 2 is wide enough to show two 'W's --- and therefore wide enough to show 3 or even 4 'n's, depending on the font. Applications that are used to show non-Latin characters will need to use a more appropriate character.

Now that we have the subclass, let's make it designable with Qt Designer. We'll take the easy "Custom Widget" approach rather than make a plugin. Start Qt Designer and click the "Tools|Custom|Edit Custom Widgets" menu option. Click the New Widget button and change the class name to "FixedLineEdit". Click the ... (three dots) button to invoke the file browser dialog, and choose the fixedlineedit.h file. Change the size hint values to 100 (width) and 25 (height). Make sure the Container Widget checkbox is unchecked. Now click the Properties tab and add two new properties, maxVisibleChars and minVisibleChars, both of type int. Click Close and you're done.

The FixedLineEdit will now appear in the "Custom Widgets" part of the toolbox. You can design with it in the same way as any other widget, and can set the properties you added in addition to the QLineEdit properties the FixedLineEdit has inherited. Qt Designer shows an icon rather than the FixedLineEdit itself; if you want to see the real widget you must make it a plugin. Making a plugin isn't difficult, and is explained in the Qt Designer manual.

The screenshot below uses QLineEdits, and the screenshot below that uses FixedLineEdits. Both forms use the same grid layout for their labels and line editors.


The QLineEdit-based form's QLineEdits expand and contract as the form is resized to take up as much width as possible.


The FixedLineEdit-based form was given sensible values for its visible characters properties, for example 30--40 for Name, 1--1 for Sex, 10--15 for City, and 2--2 for State, The FixedLineEdits will expand if the form is widened, up to their maxVisibleChars size. Similarly they will contract if the form is made narrower, down to their minVisibleChars size.

[1] If you have articles or useful snippets, let us know. We can pay for high quality articles, and have T-shirts for any code snippets we use.

This document is licensed under the Creative Commons Attribution-Share Alike 2.5 license.

Copyright © 2003 Trolltech Trademarks Internationalization Q & A »