简体   繁体   中英

Checking input of QT inputs

I have a fairly complex dialog which the inputs are numbers with different allowed ranges. I was wondering what is the cleanest pattern to guarantee that my QLineEdits have correct input values.

The obvious way of doing this seems to check input values when the user clicks the OK button. Problem I have is that some of the GUI controls depend on the value of other inputs. So the code seems to be getting a bit nasty by having me to branch the logic of the controls for all the cases where a input has a wrong value.

Is there a nice pattern for this type of situation?

I was thinking about subclassing QLineEdit and using the focusOutEvent to check for the input of the dialogs. If the input is incorrect I would default the value and trigger the logic. This would guarantee that each lineedit is responsible for it's own validation. Is there an obvious pitfall by doing this?

QValidators are awesome, problem is when their state is intermediate.

Use the signals provided by QLineEdit and build a small validation class of slots. It'll be easier than subclassing them directly, and allow you more fine grained control.

You may very well work with subclassing QLineEdit , since it is quite easy by just setting up a connection to the appropriate signals.

class foo : public QLineEdit
{
Q_OBJECT

// ... staff here

private:

void signal_control()
{
connect(this,SIGNAL(textChanged(const QString & )),this, SLOT(text_validate(const QString & )));

private slots:

void text_validate(const QString &)
{
// validate your text here
}

};

You may also build a different class that just listens to signals generated from your QLineEdit object and validates separately. Befriending it may be a good idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM