Results 1 to 3 of 3

Thread: Copying within QSqlRelationalDataModel

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

    Default Copying within QSqlRelationalDataModel

    Hi all --

    I have a small master-detail app that's worked exactly as it's supposed to - very vanilla. Click on a previously saved master/parent record (QSqlTableModel) and the detail/child records display correctly filtered (QSqlRelationalTableModel). Again, works perfectly. But now I've been asked to create a COPY mechanism wherein I select a previously created master record, and copy it's detail/child records to a newly created master record -- the only thing that will be different between the two sets of detail records is their primary key/ID will point to their master records. Make sense? I correctly have the ID of the original master, the ID of the new master, and am correctly pointing to the filtered model holding the original's detail records.

    I may be approaching this all wrong, but what I'm doing is basically doubling the detail records for the original master and changing the ID in the new records to be the new master ID. What's happening, though, is nothing is appearing in my model and nothing is being saved back out to the DB and no error is being generated anywhere -- I thought if I refreshed my view thru the filter that everything would be fine but my copied data seems to have just vanished.

    Is there a better approach? The reason I'm needing to copy is any data saved to the database can't be edited but a copy of a existing set of detail records can be (until they're saved to the DB). I've spent about 10 hours trying to work this out.

    Thanks for any non-sarcastic replies!


    scott

    My copying code:
    Qt Code:
    1. void MainWindow::on_FlowpathcopyPB_clicked()
    2. {
    3. //******************************************************************
    4. //* This functions takes the currently selected flowpath, *
    5. //* prompts for a new name, and makes a copy of the data sets... *
    6. //******************************************************************
    7. QModelIndex old_index = ui->FlowpathTV->currentIndex ();
    8. QSqlRecord old_record = flowpathmodel->record (old_index.row ());
    9. int old_fp_id = old_record.value ("flowpath_id").toInt();
    10.  
    11. if (old_index.isValid ())
    12. {
    13. on_FlowpathnewPB_clicked ();
    14.  
    15. //*****************************************************
    16. //* Here we have the ID from the copied flowpath *
    17. //* and the new ID of the new flowpath...Use *
    18. //* these to cycle thru the associated data sets... *
    19. //*****************************************************
    20. QModelIndex new_index = ui->FlowpathTV->currentIndex ();
    21. QSqlRecord new_record = flowpathmodel->record (new_index.row ());
    22. int new_fp_id = new_record.value ("flowpath_id").toInt ();
    23.  
    24. //*******************************************************
    25. //* Reset the index to the original flowdata model... *
    26. //*******************************************************
    27. ui->FlowdatanewPB->setEnabled (true);
    28. ui->FlowdatarevertPB->setEnabled (true);
    29. ui->FlowdatasavePB->setEnabled (true);
    30.  
    31. flowdatamodel->setFilter(
    32. QString("flowdata_flowpath_id = %1").arg(old_fp_id));
    33.  
    34. int num_rows_old = flowdatamodel->rowCount ();
    35. int row = num_rows_old;
    36.  
    37. for (int i = 0; i < num_rows_old; i++)
    38. {
    39. //**************************************************
    40. //* Get the records from the set to copy from... *
    41. //**************************************************
    42. old_index = flowdatamodel->index (i, 0);
    43. old_record = flowdatamodel->record (old_index.row ());
    44.  
    45. flowdatamodel->insertRow(row);
    46.  
    47. flowdatamodel->setData (flowdatamodel->index (row, 0),
    48. old_record.value("flowdata_od").toReal ());
    49. flowdatamodel->setData (flowdatamodel->index (row, 1),
    50. old_record.value("flowdata_id").toReal ());
    51. flowdatamodel->setData (flowdatamodel->index (row, 2),
    52. old_record.value("flowdate_yield").toReal ());
    53. flowdatamodel->setData (flowdatamodel->index (row, 3),
    54. old_record.value("flowdate_emod").toReal ());
    55. flowdatamodel->setData (flowdatamodel->index (row, 4),
    56. old_record.value("flowdate_val_gap").toReal ());
    57. flowdatamodel->setData (flowdatamodel->index (row, 5),
    58. old_record.value("flowdata_val_offset").toReal ());
    59. flowdatamodel->setData (flowdatamodel->index (row, 6),
    60. old_record.value("flowdate_string").toString ());
    61. flowdatamodel->setData (flowdatamodel->index (row, 7),
    62. old_record.value("flowdata_seq_no").toInt ());
    63. flowdatamodel->setData (flowdatamodel->index (row, 8), user);
    64. flowdatamodel->setData (flowdatamodel->index (row, 9),
    65. QDateTime::currentDateTime ());
    66. flowdatamodel->setData (flowdatamodel-> index (row, 10), new_fp_id);
    67.  
    68. new_index = flowdatamodel->index (row, 0);
    69. ui->FlowdataTV->setCurrentIndex (new_index);
    70. ui->FlowdataTV->edit (new_index);
    71. ui->FlowdataTV->resizeColumnsToContents ();
    72. ui->FlowdataTV->setColumnHidden (7, true);
    73. ui->FlowdataTV->setColumnHidden (10, true);
    74. ui->FlowdataTV->horizontalHeader ()->setVisible (true);
    75.  
    76. ++row;
    77. }
    78.  
    79. new_index = ui->FlowpathTV->currentIndex ();
    80.  
    81. refresh_FlowdataView ();
    82. }
    83. }
    To copy to clipboard, switch view to plain text mode 


    My code to refresh/filter the view...

    Qt Code:
    1. void MainWindow::refresh_FlowdataView()
    2. {
    3. //**********************************************************
    4. //* This function is called to display the flowdata data *
    5. //* set that are related to a particular flowpath... *
    6. //**********************************************************
    7. QModelIndex index = ui->FlowpathTV->currentIndex ();
    8.  
    9. if (index.isValid ())
    10. {
    11. QSqlRecord record = flowpathmodel->record (index.row ());
    12. int fp_id = record.value ("flowpath_id").toInt();
    13.  
    14. //*****************************************************
    15. //* Now we want to find out if this is new data *
    16. //* or not as we can't allow editing of old data... *
    17. //*****************************************************
    18. if (sqldb.numflowdatasetsDB (fp_id) > 0)
    19. {
    20. ui->FlowdatanewPB->setEnabled (false);
    21. ui->FlowdatarevertPB->setEnabled (false);
    22. ui->FlowdatasavePB->setEnabled (false);
    23. }
    24. else
    25. {
    26. ui->FlowdatanewPB->setEnabled (true);
    27. ui->FlowdatarevertPB->setEnabled (true);
    28. ui->FlowdatasavePB->setEnabled (true);
    29. }
    30.  
    31. flowdatamodel->setFilter(QString("flowdata_flowpath_id = %1").arg(fp_id));
    32. }
    33. else
    34. {
    35. flowdatamodel->setFilter ("flowdata_flowpath_id = -1");
    36. }
    37.  
    38. flowdatamodel->select ();
    39. ui->FlowdataTV->horizontalHeader ()->setVisible (
    40. flowdatamodel->rowCount () > 0);
    41. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Copying within QSqlRelationalDataModel

    Aren't you violating the uniqueness constraint on the primary key? Does insertRow() return true? What about submitAll()?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: Copying within QSqlRelationalDataModel

    Well, I tried the submitALL() and nothing happened but let me come back to that. Yep, insertRow() returns true -- just for fun, I changed line 66 to set the data to "old_fp_id" and I was able to effectively double the data saved with the dataset I'm copying. Row 1 was copied to a row 3, and row 2 copied to row 4 and off I went on my merry way.

    As for the primary key, what's happening is I select on a row in my master view (call it SNOW), say COPY, and that prompts me for the name of a new master row (call it RAIN -- this is the primary key), so that's covered. I then want to copy the detail rows linked to SNOW and link those new ones to RAIN which is what you see on line #66 of the first block of code.

    You gave me an idea about the primary key, though. My creation of *new* master and detail records (vs. copy) works great EXCEPT for something I just discovered. I have to submitALL() after the creation of the master record - if I don't and I try to create detail records, a submitALL() at this point results in no detail records being saved. Even though I'm not getting a DB error on my submitALL(), since I don't know the order that the records are being written out, there could be an issue where the child records can't get created in the DB correctly because the parent/primary key record doesn't exist yet.

    When I leave my normal job I'm going to try a submitALL after line 14 of the first block of code (which is where the new master record is being created) and see if that helps...That should write out my new master/parent record...

    Thanks for the questions -- they pulled me up out of the weeds! (BTW -- my day job is as an Oracle DB programmer so you'd think this would be a cakewalk)


    scott

Similar Threads

  1. Copying QList
    By frenk_castle in forum Newbie
    Replies: 3
    Last Post: 27th November 2009, 00:48
  2. Copying a file
    By Nefastious in forum Newbie
    Replies: 3
    Last Post: 27th October 2009, 14:15
  3. Only copying files using qmake
    By sagacity in forum Installation and Deployment
    Replies: 2
    Last Post: 26th June 2009, 14:03
  4. copying QGraphicsItemGroup
    By robertson1 in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2008, 07:48
  5. Copying row from one table to another
    By ser_bur in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2007, 15:25

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.