How to set / read the properties and limitations of a QSqlRelationalTableModel column?

Is there a way to check the properties and constraints for each column in QSqlRelationalTableModel? For example, I would like to ask my object QSqlRelationalTableModelwhether a particular column can contain zeros or which data type is valid for that column.

+3
source share
2 answers

You need to get the value QSqlFieldfor each column of the model, which is set

QSqlRecord record = model->database().record(model->tableName());
QSqlField field = record.field(columnIndex);

then you can check if the field can be empty with QSqlField::requiredStatus()(if the driver supports querying this property) and get its data type with QSqlField::type().

+2
source

alexisdm , . , -, .

gotcha: table_model::record() table_model::record(int), ( ) , , isAutoValue, , false, , . , typeID() ( , typeID()), typeID() -1 , model->database().record(model->tableName()).

  QSqlRecord record = table_model->database().record(table_model->tableName());
  // the following get isAutoValue() wrong; but have a real typeID()
  //QSqlRecord record = table_model->record();  
  //QSqlRecord record = table_model->record(table_model->rowCount() - 1);
  qDebug() << "********** table" << table_model->tableName() << "*********";
  for (int i = 0; i < table_model->columnCount(); ++i) {
    QSqlField field = record.field(i);
    qDebug() << "---------- field" << i << field.name() << "--------";
    qDebug() << "default value" << field.defaultValue();
    qDebug() << "is auto value" << field.isAutoValue();
    qDebug() << "is generated" << field.isGenerated();
    qDebug() << "is null" << field.isNull();
    qDebug() << "is read only" << field.isReadOnly();
    qDebug() << "is valid" << field.isValid();
    qDebug() << "length" << field.length();
    qDebug() << "precision" << field.precision();
    qDebug() << "required status" << field.requiredStatus();
    qDebug() << "type" << field.type();
    qDebug() << "type id" << field.typeID();
    qDebug() << "value" << field.value();
  }
+2

All Articles