Problem with QSqlField.type() (when used with QSqlTableModel)
-
I'm trying to determine the datatype of the columns (fields) of a PostgreSQL database table as rendered by QSqlTableModel. I'm using QSqlField.type() for this:
("model" is a QSqlTableModel)
@
QSqlRecord rec = model->record(1);
QSqlField f = rec.field("name");
QVariant v = f.value();
qDebug() << "Field Value for row 0 and column 1/name (should be Alex)" << v.toString();
QSqlField f2 = rec.field("name");
QVariant t = f2.type();
qDebug() << "Field Type for column 1/name (should be char)" << t.toString();
QSqlField f3 = rec.field("id");
t = f3.type();
qDebug() << "Field Type for column 0/id (should be int)" << t.toString();
@Unfortunately, this is what I get from the code above:
@
Field Value for row 0 and column 1/name (should be Alex) "Alex"
Field Type for column 1/name (should be char) ""
Field Type for column 0/id (should be int) "0"
@Does anybody know what is going wrong here?
Thanks
-
This is wrong:
@
QVariant t = f2.type();
@"QSqlField::type() ":/doc/qt-4.8/qsqlfield.html#type returns a QVariant::Type, not a QVariant!
Change your code to this:
@
QVariant::Type t = f2.type();
@The qDebug() output should print 10, which is QVariant::String.
-
Thanks! Now it works as expected:
@
Field Value for row 1 and column 1 (should be Alex) "Alex"
Field Type for column 1/name (should be char) QVariant::QString
Field Type for column 0/id (should be int) QVariant::int
@BTW, the qDebug line should be like this:
@
qDebug() << "Field Type for column 1/name (should be char)" << t;
@(No .toString() casting)
-
QVariant::Type is an enum (int) type