[Qt-interest] QLineEdit font color based upon QValidator status

Sean Harmer sean.harmer at maps-technology.com
Sun Feb 6 11:50:21 CET 2011


Hi,

On Saturday 05 February 2011 22:05:24 lbutler3 wrote:
> When QtCreator asks for a directory to put the project in the QLineEdit
> displays text in red whenever the input is invalid.  I'd like to do
> something similar in my application.  I have a QValidator-derived class
> assigned to my QLineEdit that can determine the status of the input, but
> what is the "right" way to get the QLineEdit to change font color based
> upon this status?

One way is to:

1) Add a validationStatusChanged( QValidator::State state ) signal to your 
validator.

2) When your validator gets called (ie in the validate() function) if the 
status changes from the previous time emit the above signal.

3) In your dialog class (or even in a sub-class of QLineEdit) make a 
connection between the above signal on the validator and a slot that sets the 
font colour for your line edit widget. ie something like:

MyDialog::setFontColor( QValidator::State state )
{
	switch ( state )
   {
	case QValidator::Invalid:
		// set it to red
		break;

	case QValidator::Intermediate:
		// set it to black
		break;

	case QValidator::Acceptable:
		// set it to green
		break;
	}
}

HTH,

Sean




More information about the Qt-interest-old mailing list