I have a QLineEdit which is mapped to a table column using a QDataWidgetMapper.

Qt Code:
  1. CREATE TABLE mytable (EMP_REF varchar(10) NOT NULL, EMP_NAME varchar(40));
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. model = new QSQLRelationalTableModel(this)
  2. model->setTable("mytable");
  3. model->select();
  4.  
  5. mapper = new QDataWidgetMapper(this);
  6. mapper->setModel(model);
  7. mapper->addMapping(ui->empRefEdit, 1);
To copy to clipboard, switch view to plain text mode 

The problem is that if the user does not enter a reference, the mapper simply populates the field with an empty string "" instead of a NULL and the database referential integrity is defeated.

I read another post suggesting that QLineEdit should be subclassed and a SQLText property added, but I haven't been able to work out a good way to update this property.

The QDataWidgetMapper appears not to use the text() getter method, but accesses the text property directly (I guess it has to!).

Is the only way to create a shadow SQLText property to override the event handler and update it on every keypress event? This seems a little clumsy.

Many thanks for help.