scott_hollen
5th May 2011, 17:49
Tired of pulling my hair out over this. I'm trying to take a parent/children set or records from within my sqltablemodel and copy them into a new parent/children set of data. Effectively, this:
Parent(1)
Child(1)(1)
Child(2)(1)
copied and create:
Parent(2)
Child(1)(2)
Child(2)(2)
I've done this before using filters in another application and had it working fine, but instead of displaying my data (in a typical master/detail manner) via tableviews, I'm now displaying it via a QDataWidgetMapper. This may be a red herring, though.
I'm creating the parent/child relationship via my app, which is fine, but when I try to copy, the new parent record (Parent(2)) is created correctly, but while trying to copy the children, my insertRow() command is returning a QSqlError(-1, "", "") and for the life of me I can't see why. Could be that I'm looking at it too long -- anyone see something?
ed: I checked the index of flowlinemapper after the second setfilter on flowlinemodel and it's = -1 which is a clue
//************************************************** *****
//* Get a copy of the current record, go create a new *
//* one, and copy this one's contents over to it... *
//************************************************** *****
old_region_index = regionmapper->currentIndex ();
old_region_record = regionmodel->record (old_region_index);
riser_id = old_region_record.value("region_riser_id").toInt ();
old_region_seq_no = old_region_record.value ("region_seq_no").toInt ();
row = regionmodel->rowCount ();
regionmodel->insertRow (row);
//******************************************
//* Get hold of the new sequence number *
//* because we're gonna need it later... *
//******************************************
new_region_seq_no = sqldb.generate_seq_noDB ("REGION");
regionmodel->setData (regionmodel->index (row, 33), new_region_seq_no);
regionmodel->setData (regionmodel->index (row, 34), riser_id);
regionmodel->setData (regionmodel->index (row, 35), user);
regionmodel->setData (regionmodel->index (row, 36),
QDateTime::currentDateTime ());
//************************************************** *************
//* Now copy the flowline data associated with this region... *
//* We have to make sure we reset the flowline data to the *
//* first record associated with this region and loop thru *
//* all those records, copying them to the new set of data... *
//************************************************** *************
flowlinemapper->toFirst ();
old_flowline_rowcount = flowlinemodel->rowCount ();
for (int i = 0; i < old_flowline_rowcount; i++)
{
flowline_filter = QString("flowline_riser_id = %1").arg (riser_id);
flowline_filter.append (" and ");
flowline_filter.append (QString("flowline_region_seq_no = %2").
arg (old_region_seq_no));
flowlinemodel->setFilter (flowline_filter);
//* This correctly returns the record to be copied...
old_flowline_record = flowlinemodel->record (i);
flowline_filter = QString("flowline_riser_id = %1").arg (riser_id);
flowline_filter.append (" and ");
flowline_filter.append (QString("flowline_region_seq_no = %2").
arg (new_region_seq_no));
flowlinemodel->setFilter (flowline_filter);
if (!flowlinemodel->insertRow (i))
qDebug() << flowlinemodel->lastError ();
else
{
flowlinemodel->setData (flowlinemodel->index (i, 7),
sqldb.generate_seq_noDB ("FLOWLINE"));
flowlinemodel->setData (flowlinemodel->index (i, 8), new_region_seq_no);
flowlinemodel->setData (flowlinemodel->index (i, 9), riser_id);
flowlinemodel->setData (flowlinemodel->index (i, 10), user);
flowlinemodel->setData (flowlinemodel->index (i, 11),
QDateTime::currentDateTime ());
}
}
Thanks!
scott
Parent(1)
Child(1)(1)
Child(2)(1)
copied and create:
Parent(2)
Child(1)(2)
Child(2)(2)
I've done this before using filters in another application and had it working fine, but instead of displaying my data (in a typical master/detail manner) via tableviews, I'm now displaying it via a QDataWidgetMapper. This may be a red herring, though.
I'm creating the parent/child relationship via my app, which is fine, but when I try to copy, the new parent record (Parent(2)) is created correctly, but while trying to copy the children, my insertRow() command is returning a QSqlError(-1, "", "") and for the life of me I can't see why. Could be that I'm looking at it too long -- anyone see something?
ed: I checked the index of flowlinemapper after the second setfilter on flowlinemodel and it's = -1 which is a clue
//************************************************** *****
//* Get a copy of the current record, go create a new *
//* one, and copy this one's contents over to it... *
//************************************************** *****
old_region_index = regionmapper->currentIndex ();
old_region_record = regionmodel->record (old_region_index);
riser_id = old_region_record.value("region_riser_id").toInt ();
old_region_seq_no = old_region_record.value ("region_seq_no").toInt ();
row = regionmodel->rowCount ();
regionmodel->insertRow (row);
//******************************************
//* Get hold of the new sequence number *
//* because we're gonna need it later... *
//******************************************
new_region_seq_no = sqldb.generate_seq_noDB ("REGION");
regionmodel->setData (regionmodel->index (row, 33), new_region_seq_no);
regionmodel->setData (regionmodel->index (row, 34), riser_id);
regionmodel->setData (regionmodel->index (row, 35), user);
regionmodel->setData (regionmodel->index (row, 36),
QDateTime::currentDateTime ());
//************************************************** *************
//* Now copy the flowline data associated with this region... *
//* We have to make sure we reset the flowline data to the *
//* first record associated with this region and loop thru *
//* all those records, copying them to the new set of data... *
//************************************************** *************
flowlinemapper->toFirst ();
old_flowline_rowcount = flowlinemodel->rowCount ();
for (int i = 0; i < old_flowline_rowcount; i++)
{
flowline_filter = QString("flowline_riser_id = %1").arg (riser_id);
flowline_filter.append (" and ");
flowline_filter.append (QString("flowline_region_seq_no = %2").
arg (old_region_seq_no));
flowlinemodel->setFilter (flowline_filter);
//* This correctly returns the record to be copied...
old_flowline_record = flowlinemodel->record (i);
flowline_filter = QString("flowline_riser_id = %1").arg (riser_id);
flowline_filter.append (" and ");
flowline_filter.append (QString("flowline_region_seq_no = %2").
arg (new_region_seq_no));
flowlinemodel->setFilter (flowline_filter);
if (!flowlinemodel->insertRow (i))
qDebug() << flowlinemodel->lastError ();
else
{
flowlinemodel->setData (flowlinemodel->index (i, 7),
sqldb.generate_seq_noDB ("FLOWLINE"));
flowlinemodel->setData (flowlinemodel->index (i, 8), new_region_seq_no);
flowlinemodel->setData (flowlinemodel->index (i, 9), riser_id);
flowlinemodel->setData (flowlinemodel->index (i, 10), user);
flowlinemodel->setData (flowlinemodel->index (i, 11),
QDateTime::currentDateTime ());
}
}
Thanks!
scott