[Kde-java] A curious problem with QSqlCursor

David Goodenough david.goodenough at btconnect.com
Mon Oct 25 10:26:10 CEST 2004


Richard,

I have been working further with the java interface, and I have hit a 
problem and I am not quite sure how to proceed.

According to the Qt SQL documentation in order to get a calculated field
into a QDataTable or QDataBrowser you have to subclass QSqlCursor
and in the constructor of the subclass append the new fields and set
them as calculated and then have a calculateField method which 
does the calculation.

This is all described in http://doc.trolltech.com/3.3/sql.html

This I did:-

public class ChargeCursor extends QSqlCursor {
 public ChargeCursor() {
  super( "charges");
  setMode( ReadOnly); // we only read data through this cursor
  QSqlFieldInfo vat = new QSqlFieldInfo( "vat", QVariant.Double);
  vat.setCalculated( true);
  append( vat);
  QSqlFieldInfo total = new QSqlFieldInfo( "total", QVariant.Double);
  total.setCalculated( true);
  append( total);
  }
 public QVariant calculateField( String name) {
  System.out.println( "Calculating field " + name);
  if( "vat".equals( name)) 
   {
    double price = value( "price").toDouble( );
    double vatrate = value( "vatrate").toDouble( );
    return new QVariant( price * vatrate);
    }
   else if( "total".equals( name)) {
    double price = value( "price").toDouble( );
    double vatrate = value( "vatrate").toDouble( );
    return new QVariant( price * ( 1 + vatrate));
    }
  return new QVariant( ( String)null);
  }
 }

When I try to use this subclassed cursor however it does not seem to be 
calling the calculateField method and when you use somthing like the
QDataTable.refresh method a dialog pops up complaining that charges.vat
does not exist.  

When I come to use the ChargeCursor even simply getting the value does
NOT seem to call calculateField (which you will notice has a debug line in it 
above).

  cursor = new ChargeCursor( );
  System.out.println( "cursor.value( 'vat') = " + cursor.value( "vat"));
  chargesTable.setSqlCursor( cursor, false, false); 
  System.out.println( "Set SqlCursor");
  chargesTable.setColumn( 0, "item", "Item");
  chargesTable.setColumn( 1, "quantity", "Quantity");
  chargesTable.setColumn( 2, "description", "Description");
  chargesTable.setColumn( 3, "price", "Price");
  chargesTable.setColumn( 4, "vat", "VAT");
  chargesTable.setColumn( 5, "due", "Due Date");
  chargesTable.setColumn( 6, "total", "Total");
  chargesTable.setFilter( "customer = '" + customer + "' AND invoice = ''");
  String[ ]sort = { "due DESC" };
  chargesTable.setSort( sort);
  chargesTable.refresh( );

It is at this point (the call to refresh) when the message about column 
charges.vat does not exist comes up.

I have checked that the calculatedField attribute is being properly set.  

So my question is, where do I start looking for why the callback is not
being called?  

David


More information about the Kde-java mailing list