Results 1 to 3 of 3

Thread: Copying data within model/widgetmapper

  1. #1
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Copying data within model/widgetmapper

    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

    Qt Code:
    1. //*******************************************************
    2. //* Get a copy of the current record, go create a new *
    3. //* one, and copy this one's contents over to it... *
    4. //*******************************************************
    5. old_region_index = regionmapper->currentIndex ();
    6. old_region_record = regionmodel->record (old_region_index);
    7.  
    8. riser_id = old_region_record.value("region_riser_id").toInt ();
    9. old_region_seq_no = old_region_record.value ("region_seq_no").toInt ();
    10.  
    11.  
    12. row = regionmodel->rowCount ();
    13. regionmodel->insertRow (row);
    14.  
    15. //******************************************
    16. //* Get hold of the new sequence number *
    17. //* because we're gonna need it later... *
    18. //******************************************
    19. new_region_seq_no = sqldb.generate_seq_noDB ("REGION");
    20.  
    21. regionmodel->setData (regionmodel->index (row, 33), new_region_seq_no);
    22. regionmodel->setData (regionmodel->index (row, 34), riser_id);
    23. regionmodel->setData (regionmodel->index (row, 35), user);
    24. regionmodel->setData (regionmodel->index (row, 36),
    25. QDateTime::currentDateTime ());
    26.  
    27.  
    28. //***************************************************************
    29. //* Now copy the flowline data associated with this region... *
    30. //* We have to make sure we reset the flowline data to the *
    31. //* first record associated with this region and loop thru *
    32. //* all those records, copying them to the new set of data... *
    33. //***************************************************************
    34. flowlinemapper->toFirst ();
    35. old_flowline_rowcount = flowlinemodel->rowCount ();
    36.  
    37. for (int i = 0; i < old_flowline_rowcount; i++)
    38. {
    39. flowline_filter = QString("flowline_riser_id = %1").arg (riser_id);
    40. flowline_filter.append (" and ");
    41. flowline_filter.append (QString("flowline_region_seq_no = %2").
    42. arg (old_region_seq_no));
    43. flowlinemodel->setFilter (flowline_filter);
    44.  
    45. //* This correctly returns the record to be copied...
    46. old_flowline_record = flowlinemodel->record (i);
    47.  
    48. flowline_filter = QString("flowline_riser_id = %1").arg (riser_id);
    49. flowline_filter.append (" and ");
    50. flowline_filter.append (QString("flowline_region_seq_no = %2").
    51. arg (new_region_seq_no));
    52. flowlinemodel->setFilter (flowline_filter);
    53.  
    54. if (!flowlinemodel->insertRow (i))
    55. qDebug() << flowlinemodel->lastError ();
    56. else
    57. {
    58. flowlinemodel->setData (flowlinemodel->index (i, 7),
    59. sqldb.generate_seq_noDB ("FLOWLINE"));
    60. flowlinemodel->setData (flowlinemodel->index (i, 8), new_region_seq_no);
    61. flowlinemodel->setData (flowlinemodel->index (i, 9), riser_id);
    62. flowlinemodel->setData (flowlinemodel->index (i, 10), user);
    63. flowlinemodel->setData (flowlinemodel->index (i, 11),
    64. QDateTime::currentDateTime ());
    65. }
    66. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!


    scott
    Last edited by scott_hollen; 5th May 2011 at 20:48.

  2. #2
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Copying data within model/widgetmapper

    Slight revision: if I'm copying 1 child it works now, but not for more than 1:

    Qt Code:
    1. regionmodel->setData (regionmodel->index (row, 33), new_region_seq_no);
    2. regionmodel->setData (regionmodel->index (row, 34), riser_id);
    3. regionmodel->setData (regionmodel->index (row, 35), user);
    4. regionmodel->setData (regionmodel->index (row, 36),
    5. QDateTime::currentDateTime ());
    6.  
    7. regionmapper->setCurrentIndex (row);
    8. new_region_index = regionmapper->currentIndex ();
    9.  
    10. flowline_filter = QString("flowline_riser_id = %1").arg (riser_id);
    11. flowline_filter.append (" and ");
    12. flowline_filter.append (QString("flowline_region_seq_no = %2").
    13. arg (new_region_seq_no));
    14. flowlinemodel->setFilter (flowline_filter);
    15. flowlinemapper->toFirst ();
    16.  
    17. //***************************************************************
    18. //* Now copy the flowline data associated with this region... *
    19. //* We have to make sure we reset the flowline data to the *
    20. //* first record associated with this region and loop thru *
    21. //* all those records, copying them to the new set of data... *
    22. //***************************************************************
    23. flowlinemapper->toFirst ();
    24.  
    25. for (int i = 0; i < old_flowline_rowcount; i++)
    26. {
    27. regionmapper->setCurrentIndex (old_region_index);
    28.  
    29. flowline_filter = QString("flowline_riser_id = %1").arg (riser_id);
    30. flowline_filter.append (" and ");
    31. flowline_filter.append (QString("flowline_region_seq_no = %2").
    32. arg (old_region_seq_no));
    33. flowlinemodel->setFilter (flowline_filter);
    34.  
    35. old_flowline_record = flowlinemodel->record (i);
    36.  
    37. regionmapper->setCurrentIndex (new_region_index);
    38.  
    39. flowline_filter = QString("flowline_riser_id = %1").arg (riser_id);
    40. flowline_filter.append (" and ");
    41. flowline_filter.append (QString("flowline_region_seq_no = %2").
    42. arg (new_region_seq_no));
    43. flowlinemodel->setFilter (flowline_filter);
    44.  
    45. if (!flowlinemodel->insertRow (i))
    46. qDebug() << "error" << i;
    47. {
    48. flowlinemapper->setCurrentIndex (i);
    49.  
    50. flowlinemodel->setData (flowlinemodel->index(i, 0),
    51. old_flowline_record.value ("flowline_od").toString ());
    52. flowlinemodel->setData (flowlinemodel->index(i, 1),
    53. old_flowline_record.value ("flowline_id").toString ());
    54. flowlinemodel->setData (flowlinemodel->index(i, 2),
    55. old_flowline_record.value ("flowline_yield").toString ());
    56. flowlinemodel->setData (flowlinemodel->index(i, 3),
    57. old_flowline_record.value ("flowline_emod").toString ());
    58. flowlinemodel->setData (flowlinemodel->index(i, 4),
    59. old_flowline_record.value ("flowline_val_gap").toString ());
    60. flowlinemodel->setData (flowlinemodel->index(i, 5),
    61. old_flowline_record.value ("flowline_val_offset").toString ());
    62. flowlinemodel->setData (flowlinemodel->index(i, 6),
    63. old_flowline_record.value ("flowline_string").toString ());
    64. flowlinemodel->setData (flowlinemodel->index (i, 7),
    65. sqldb.generate_seq_noDB ("FLOWLINE"));
    66. flowlinemodel->setData (flowlinemodel->index (i, 8), new_region_seq_no);
    67. flowlinemodel->setData (flowlinemodel->index (i, 9), riser_id);
    68. flowlinemodel->setData (flowlinemodel->index (i, 10), user);
    69. flowlinemodel->setData (flowlinemodel->index (i, 11),
    70. QDateTime::currentDateTime ());
    71. }
    72. }
    73.  
    74. flowlinemapper->toFirst ();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2011
    Location
    Richmond, VA
    Posts
    94
    Thanks
    14
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Copying data within model/widgetmapper

    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

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 20:39
  2. Replies: 1
    Last Post: 24th February 2011, 06:54
  3. memory leak when copying Qimage data
    By klobauster_toomc in forum Qt Programming
    Replies: 2
    Last Post: 25th February 2010, 09:33
  4. Data model
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2007, 13:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.