Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, January 2008
Determining the type of a SQL column


Message 1 in thread

Hi people !

I'm writing a wigdet extending QTableView to make a custom table viewer
specialized for displaying SQL data. More particulary I would like to
manage boolean SQL fields using a QCheckBox. Delegates should be suitable
for this purpose but before that I need to determine the SQL type of each
column in my table model.

I do not find any method in QSqlTableModel nor in QSqlQueryModel (where it
should be located IMHO) to do this, so I write this piece of code :

  for (int i=0 ; i< model.columnCount() ; i++){
    Object obj = model.data(model.index(0,i), Qt.ItemDataRole.DisplayRole);
    if(obj instanceof Boolean){
      System.out.println("boolean object");
    }
  }

It works fine, but I'm far from certain this is the best manner to do the
trick. Several reasons :
1- Doesn't work if model isn't populated or if sql table is empty. I
didn't verify this but I suppose model.index() is null for empty set.
However sql column type exists even if the table is empty.
2- Based on a single row while sql column type is a column attribute more
than a value attribute
3- Uses role information which have nothing to do with SQL column type.

So my question is quite simple : is there is a better way to do this ?

Thanks for any hint.

jMax



Message 2 in thread

moebius@xxxxxxxxxx wrote:
> Hi people !

Hi again,

This functionality is not obvious in the current documentation, but it 
should be done something like this:

Once you have QSqlRecord you can check the various field types which 
represents the various columnns, using the QSqlRecord.field() method. 
Once you have the field of the column you can check its type using the 
QSqlField.type() method. This represents the Qt type id number and is a 
set of integers listed in com.trolltech.qt.QVariant.

so you would do:

   if (field.type() == QVariant.Boolean) {
     // do boolean logic...
   }

best regards,
Gunnar

> 
> I'm writing a wigdet extending QTableView to make a custom table viewer
> specialized for displaying SQL data. More particulary I would like to
> manage boolean SQL fields using a QCheckBox. Delegates should be suitable
> for this purpose but before that I need to determine the SQL type of each
> column in my table model.
> 
> I do not find any method in QSqlTableModel nor in QSqlQueryModel (where it
> should be located IMHO) to do this, so I write this piece of code :
> 
>   for (int i=0 ; i< model.columnCount() ; i++){
>     Object obj = model.data(model.index(0,i), Qt.ItemDataRole.DisplayRole);
>     if(obj instanceof Boolean){
>       System.out.println("boolean object");
>     }
>   }
> 
> It works fine, but I'm far from certain this is the best manner to do the
> trick. Several reasons :
> 1- Doesn't work if model isn't populated or if sql table is empty. I
> didn't verify this but I suppose model.index() is null for empty set.
> However sql column type exists even if the table is empty.
> 2- Based on a single row while sql column type is a column attribute more
> than a value attribute
> 3- Uses role information which have nothing to do with SQL column type.
> 
> So my question is quite simple : is there is a better way to do this ?
> 
> Thanks for any hint.
> 
> jMax
> 
> 


Message 3 in thread

> moebius@xxxxxxxxxx wrote:
>> Hi people !
>
> Hi again,
>
> This functionality is not obvious in the current documentation, but it
> should be done something like this:
>
> Once you have QSqlRecord you can check the various field types which
> represents the various columnns, using the QSqlRecord.field() method.
> Once you have the field of the column you can check its type using the
> QSqlField.type() method. This represents the Qt type id number and is a
> set of integers listed in com.trolltech.qt.QVariant.
>
> so you would do:
>
>    if (field.type() == QVariant.Boolean) {
>      // do boolean logic...
>    }
>
> best regards,
> Gunnar
>
>>
>> I'm writing a wigdet extending QTableView to make a custom table viewer
>> specialized for displaying SQL data. More particulary I would like to
>> manage boolean SQL fields using a QCheckBox. Delegates should be
>> suitable
>> for this purpose but before that I need to determine the SQL type of
>> each
>> column in my table model.
>>
>> I do not find any method in QSqlTableModel nor in QSqlQueryModel (where
>> it
>> should be located IMHO) to do this, so I write this piece of code :
>>
>>   for (int i=0 ; i< model.columnCount() ; i++){
>>     Object obj = model.data(model.index(0,i),
>> Qt.ItemDataRole.DisplayRole);
>>     if(obj instanceof Boolean){
>>       System.out.println("boolean object");
>>     }
>>   }
>>
>> It works fine, but I'm far from certain this is the best manner to do
>> the
>> trick. Several reasons :
>> 1- Doesn't work if model isn't populated or if sql table is empty. I
>> didn't verify this but I suppose model.index() is null for empty set.
>> However sql column type exists even if the table is empty.
>> 2- Based on a single row while sql column type is a column attribute
>> more
>> than a value attribute
>> 3- Uses role information which have nothing to do with SQL column type.
>>
>> So my question is quite simple : is there is a better way to do this ?
>>
>> Thanks for any hint.
>>
>> jMax
>>
>>
>
>

Hello !

Thanks to you Gunnar, I can do it in very more pleasant fashion.

To get the QSqlRecord, I use the appropriate function in QSqlQueryModel
(quite the same is present in QSqlDatabase, but this one is more suitable
in my context)

so my ugly previous code becomes :

QSqlRecord columns = ((QSqlQueryModel)model).record();
for (int i = 0; i <columns.count() ; i++){
  if(columns.field(i).type() == QVariant.Boolean)
	// here I do procedure specific to boolean columns
}

This should work even with empty tables. Of course this case may not arise
but, who knows...

Anyway thanks again for your response.

jMax