tags:

views:

298

answers:

2

What is the best starting point for a Qt4 widget for entering Latitude/Longitude in DD:MM:SS format (degrees, minutes, seconds)? Customize a QLineEdit? A series of spin boxes?

+2  A: 

I would use a QValidator, attaching it to a QLineEdit using QLineEdit::setValidator().

You'll need to subclass so you can implement the validate() function and possibly the fixup() function for your particular case, since the two validators included with Qt only cover integers and doubles.

It's a bit friendlier, in my opinion, to provide a single input box for this rather than three separate spin boxes (which could look cluttered and isn't as nice to type in).

[Edit: One other alternative is to set a "validation input mask" on your QLineEdit using QLineEdit::setInputMask(). You might want a line edit with symbols already in place and placeholders for other characters, for example, and this approach will give you something similar to that. The QtDemo app has an example of this which you can check out by choosing Widgets->Line Edits (Widgets is on the second page).]

richardwb
+4  A: 

There are some variants:

QLineEdit with Validator - wasn't good enough for us, we couldn't achieve usable editing and proper view (with ', '' and degree symbols in place and ability to forbid incorrect values and still allow semi-correct states, and the target behaviour is not to mark errors and force user to fix them, but to allow user to enter only valid values).

Three spin edits in a line with the proper symbols between them grouped as a single widget and some code to move keyboard input from one no next when needed etc. Looks good enough in some cases, and you can find the variant of realization in the famous Marble project.

Still, my boss said that this approach is almost as ugly as first, so here is another approach: subclass QAbstractSpinBox, as Trolltech did in their QDateTimeEditor. In fact, behaviour of such a widget is near similar to one, implemented in QDateTimeEditor. I, myself didn't do it yet, cause of task priorities, but will have to do.

Max
Have you looked at QLineEdit's input mask support? I've updated my answer with that alternative as well.
richardwb
Yes, I'd try this variant too, the problem here is the fact, that input mask tells widget, that some symbols are proper at some positions, that is not true in our case: maybe there is some mix of validator and input mask logic, that will both draw degree and minute/second mark in proper place and handle proper +/-/digit positions and range issues.More to say, I need control, that will handle different coordinate formats (d.m.s, d.m, where m is decimal and distances in meters)
Max