PDA

View Full Version : Copying data within model/widgetmapper



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

scott_hollen
6th May 2011, 00:46
Slight revision: if I'm copying 1 child it works now, but not for more than 1:


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 ());

regionmapper->setCurrentIndex (row);
new_region_index = regionmapper->currentIndex ();

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);
flowlinemapper->toFirst ();

//************************************************** *************
//* 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 ();

for (int i = 0; i < old_flowline_rowcount; i++)
{
regionmapper->setCurrentIndex (old_region_index);

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);

old_flowline_record = flowlinemodel->record (i);

regionmapper->setCurrentIndex (new_region_index);

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() << "error" << i;
{
flowlinemapper->setCurrentIndex (i);

flowlinemodel->setData (flowlinemodel->index(i, 0),
old_flowline_record.value ("flowline_od").toString ());
flowlinemodel->setData (flowlinemodel->index(i, 1),
old_flowline_record.value ("flowline_id").toString ());
flowlinemodel->setData (flowlinemodel->index(i, 2),
old_flowline_record.value ("flowline_yield").toString ());
flowlinemodel->setData (flowlinemodel->index(i, 3),
old_flowline_record.value ("flowline_emod").toString ());
flowlinemodel->setData (flowlinemodel->index(i, 4),
old_flowline_record.value ("flowline_val_gap").toString ());
flowlinemodel->setData (flowlinemodel->index(i, 5),
old_flowline_record.value ("flowline_val_offset").toString ());
flowlinemodel->setData (flowlinemodel->index(i, 6),
old_flowline_record.value ("flowline_string").toString ());
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 ());
}
}

flowlinemapper->toFirst ();

scott_hollen
6th May 2011, 18:27
Oh, my God...I can't believe but in each loop I reset the CurrentIndex to the same starting point so on my second loop I'm attempting to insert over the first record...

You'd think after nearly 30 years of programming mistakes like this wouldn't happen...


scott