Results 1 to 7 of 7

Thread: QSqlTableModel issues in 4.8.1 vs. 4.7.4?

  1. #1
    Join Date
    Jan 2012
    Posts
    46
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QSqlTableModel issues in 4.8.1 vs. 4.7.4?

    Good afternoon --

    I was wondering if anyone has noticed in behavioral differences when using QSqlTableModels in 4.8.1 vs. 4.7.4? I recently upgraded my Mac and downloaded the 4.8.1 version of Qt and I'm noticing some squirrely things that I've verified don't occur in 4.7.4

    I have a lot of models in my app with 4 of them mapped to combo boxes. 2 of them work fine, but with 2 of the others, while the data initially appears correct in the combo box, if I click on an "edit" button beside the CB, the first record is replaced with the current index of the value I'm editing. For instance with the CB that controls data displayed throughout the app this is what I'm seeing:

    Initially
    Rig One
    Rig Two --> current index

    After I click edit, which generates an QInputDialog which I cancel out of
    1
    Rig Two

    If I choose to edit "1" and cancel, it changes to "0"

    For the other CB that's acting weird the data I'm editing is the data that's replaced with the index, so editing the 5 item in the list changes from "Ladder" to "4" and this happens immediately upon the QInputDialog being displayed (I can see the CB change).


    What I've noticed via debugging is that the value changes after the QInputDialog is generated. Initially when this problem appeared it was happening when I clicked any pushbutton widget but I seem to have eliminated that (don't really know how). It's also strange that the other two CBs are working fine and they're all defined (at least to my eyes) in the same manner.

    Qt Code:
    1. //*****************
    2. //* Rig Data... *
    3. //*****************
    4. rigmodel = new QSqlTableModel(this, sqldb.db);
    5.  
    6. rigmodel->setTable ("rig");
    7. rigmodel->sort (1, Qt::AscendingOrder);
    8. rigmodel->setEditStrategy (QSqlTableModel::OnManualSubmit);
    9.  
    10. rigmodel->select ();
    11.  
    12. //*********************************************
    13. //* This is the UI for editing Rig data... *
    14. //*********************************************
    15. ui->RigAdminNameCB->setModel (rigmodel);
    16. ui->RigAdminNameCB->setModelColumn (1);
    17. ui->RigAdminNameCB->setCurrentIndex (0);
    18.  
    19. rigmapper = new QDataWidgetMapper(this);
    20. rigmapper->setModel (rigmodel);
    21.  
    22. rigmapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    23. rigmapper->addMapping(ui->RigAdminNameCB, 1);
    24. rigmapper->addMapping(ui->RigTypeDrillShipRB, 2);
    25. rigmapper->addMapping(ui->RigLengthOverallLE, 3);
    26. rigmapper->addMapping(ui->RigLengthOverallFtRB, 4);
    27. rigmapper->addMapping(ui->RigLengthK2FLE, 5);
    28. rigmapper->addMapping(ui->RigLengthK2FFtRB, 6);
    29. rigmapper->setCurrentIndex(0);
    30.  
    31.  
    32. //******************************
    33. //* Simulation Data setup... *
    34. //******************************
    35. simulationmodel = new QSqlTableModel(this, sqldb.db);
    36.  
    37. simulationmodel->setTable ("simulation");
    38. simulationmodel->sort (1, Qt::AscendingOrder);
    39. simulationmodel->setEditStrategy (QSqlTableModel::OnManualSubmit);
    40. simulationmodel->setFilter (QString("sim_rig_id = %1").arg(rig_id));
    41.  
    42. simulationmodel->select ();
    43.  
    44. //***************************************************
    45. //* This is the UI for editing Simulation data... *
    46. //***************************************************
    47. ui->SimNameCB->setModel (simulationmodel);
    48. ui->SimNameCB->setModelColumn (1);
    49. ui->SimNameCB->setCurrentIndex (0);
    50.  
    51. simulationmapper = new QDataWidgetMapper(this);
    52. simulationmapper->setModel (simulationmodel);
    53.  
    54. simulationmapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    55. simulationmapper->addMapping (ui->SimNameCB, 1);
    56. simulationmapper->setCurrentIndex(0);
    57.  
    58.  
    59. //**********************
    60. //* Riser ID data... *
    61. //**********************
    62. riseridmodel = new QSqlTableModel(this, sqldb.db);
    63.  
    64. riseridmodel->setTable ("riser");
    65. riseridmodel->sort (1, Qt::AscendingOrder);
    66. riseridmodel->setEditStrategy (QSqlTableModel::OnManualSubmit);
    67.  
    68. riseridmodel->setFilter (QString("riser_rig_id = %1").arg(rig_id));
    69.  
    70. riseridmodel->select ();
    71.  
    72. //**********************************************
    73. //* This is the UI for editing Riser data... *
    74. //**********************************************
    75. ui->RiserConfigCB->setModel (riseridmodel);
    76. ui->RiserConfigCB->setModelColumn (1);
    77.  
    78. //*************************************************************
    79. //* Find the Riser Model for the Simulation being displayed *
    80. //* and set the current index to that record... *
    81. //*************************************************************
    82. for (riser_index = 0; riser_index < riseridmodel->rowCount ();
    83. riser_index++)
    84. {
    85. record = riseridmodel->record(riser_index);
    86. if (record.value ("riser_id").toInt () == sim_riser_id)
    87. break;
    88. }
    89.  
    90. ui->RiserConfigCB->setCurrentIndex (riser_index);
    91.  
    92. riseridmapper = new QDataWidgetMapper(this);
    93. riseridmapper->setModel (riseridmodel);
    94.  
    95. riseridmapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    96. riseridmapper->addMapping (ui->RiserConfigCB, 1);
    97. riseridmapper->setCurrentIndex (riser_index);
    98.  
    99.  
    100. //***************************
    101. //* Rig Inventory Data... *
    102. //***************************
    103. riginvmodel = new QSqlTableModel(this, sqldb.db);
    104.  
    105. riginvmodel->setTable ("riginv");
    106. riginvmodel->sort (33, Qt::AscendingOrder);
    107. riginvmodel->setEditStrategy (QSqlTableModel::OnManualSubmit);
    108.  
    109. riginvmodel->setFilter (QString("riginv_rig_id = %1").arg(rig_id));
    110. riginvmodel->select ();
    111.  
    112. //**************************************************
    113. //* This is the UI for editing Inventory data... *
    114. //**************************************************
    115. ui->EquipIDCB->setModel (riginvmodel);
    116. ui->EquipIDCB->setModelColumn (33);
    117. ui->EquipIDCB->setCurrentIndex (0);
    118.  
    119. riginvmapper = new QDataWidgetMapper(this);
    120. riginvmapper->setModel (riginvmodel);
    121.  
    122. riginvmapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    123. riginvmapper->addMapping (ui->EquipIDCB, 33);
    124.  
    125. riginvmapper->setCurrentIndex(0);
    To copy to clipboard, switch view to plain text mode 

    Here is the code where the first CB goes south:

    Qt Code:
    1. void MainWindow::on_EditRigAdminPB_clicked()
    2. {
    3. bool ok;
    4. int current_index;
    5.  
    6. QString oldrig_name;
    7. QString newrig_name;
    8. QString message;
    9. QSqlRecord rig_record;
    10.  
    11. current_index = ui->RigAdminNameCB->currentIndex ();
    12. rig_record = rigmodel->record (current_index);
    13. oldrig_name = rig_record.value("rig_name").toString();
    14.  
    15. //***********************************
    16. //* Go get a name for this rig... *
    17. //***********************************
    18.  
    19. // if I display the data from the model at this point it's fine...
    20.  
    21. message = "Enter a New Name for this Rig: " + oldrig_name;
    22. newrig_name = QInputDialog::getText (this, "Rig Name Change",
    23. message, QLineEdit::Normal, QString(), &ok);
    24.  
    25. if (ok && !newrig_name.isEmpty ())
    26. {
    27. if (dup_rig_name_check (newrig_name))
    28. {
    29. QMessageBox::warning (this, tr("Rig Name exists"),
    30. tr("A Rig data set of this name "
    31. "already exists. Please re-enter."));
    32. }
    33. else
    34. {
    35. rigmodel->setData (rigmodel->index (current_index, 1), newrig_name);
    36. rigmodel->setData (rigmodel->index (current_index, 6), user);
    37. rigmodel->setData (rigmodel->index (current_index, 7),
    38. QDateTime::currentDateTime ());
    39.  
    40. emit set_save_flag();
    41. }
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    I've verified that apps works correctly on 4.7.4 in both Windows 7 and Mac 10.7.4 , but has this problem in 4.8.1 on both Windows and Mac.

    Anyone witness anything like this? As you can see, the code is pretty straight forward and the fact that it works in a different version of Qt leaves me scratching my head

    Thanks!


    Kodi




    The Riser and Simulation models are the ones that work correctly -- the Rig and Rig Inventory are the ones that don't.
    Last edited by mtnbiker66; 12th July 2012 at 01:00.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlTableModel issues in 4.8.1 vs. 4.7.4?

    What does the signal set_save_flag() trigger?

  3. #3
    Join Date
    Jan 2012
    Posts
    46
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QSqlTableModel issues in 4.8.1 vs. 4.7.4?

    It enables a pushbutton for "saving" the data

    Qt Code:
    1. void MainWindow::set_save_flag ()
    2. {
    3. ui->actionSave_All->setEnabled(true);
    4.  
    5. need_to_save_flag = true;
    6. }
    To copy to clipboard, switch view to plain text mode 


    Also, if I output the model data via a qDebug statement before "if (ok && !newrig_name.isEmpty ())", I can see that the data has changed...


    Kodi

    ps - thanks for asking!
    Last edited by mtnbiker66; 12th July 2012 at 01:03.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlTableModel issues in 4.8.1 vs. 4.7.4?

    If the data in the model has changed before line 25, i.e. before any setData() calls, then the problem is not in the code I can see.


    Also, see if the issue is related to https://bugreports.qt-project.org/browse/QTBUG-10491 (it is not exactly this though)

  5. The following user says thank you to ChrisW67 for this useful post:

    mtnbiker66 (12th July 2012)

  6. #5
    Join Date
    Jan 2012
    Posts
    46
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QSqlTableModel issues in 4.8.1 vs. 4.7.4?

    I can see the data change at the same time the QInputdialog gets displayed.

    Thanks for the link about the bug -- I searched thru the bug reports but came back with nothing before I made this post, but that sounds like a really good possibility. I think what I'll do is change my database table structure to put the displayed column being the first instead of the second and see what happens.

    Thanks for the extra set of eyes -- I didn't think I was going crazy since it works in a previous Qt version.


    Kodi


    ed. I changed the combo box to point to the first column in DB table, and it appeared to take care of the problem because the values in the CB stayed constant, however looking at the raw data, the 2nd column in the table -- the field previously displayed -- showed the change was still taking place. The data changed from "Rig One" to "0".
    Last edited by mtnbiker66; 12th July 2012 at 18:57.

  7. #6
    Join Date
    Jan 2012
    Posts
    46
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QSqlTableModel issues in 4.8.1 vs. 4.7.4?

    Back and forth with Qt on this -- the official answer, bolding mine:

    ...The data widget mapper "detects" the change in the combo box and updates the model via QSqlTable::setData(). The model has edit strategy OnManualSubmit, so changes are not submitted to the database right away. However, the model does emit dataChanged() which is received by the combo box and the mapper in an undefined order. If the list in the combo box gets updated first, consider yourself lucky.

    Another important point concerns the default item delegate used by the mapper. It links the mapped model column to the "currentIndex" property (row) of the combo box while one might expect it would use the "currentText". This is why you see the number 0 set in the model instead of the text at that index.

    Rejecting as "invalid" because the reported behavior is actually correct. Arguably, the default behavior of the item delegate with a combobox is suboptimal, but it's not quite a bug and there are documented ways to get more useful behavior.

    QTBUG-26501

  8. #7
    Join Date
    Jan 2012
    Posts
    46
    Thanks
    11
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QSqlTableModel issues in 4.8.1 vs. 4.7.4?


Similar Threads

  1. [Solved] QSqlTableModel issues..
    By Nedlinin in forum Qt Programming
    Replies: 5
    Last Post: 4th November 2011, 15:06
  2. Replies: 8
    Last Post: 30th March 2011, 20:06
  3. QSqlTableModel
    By codeman in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2009, 11:04
  4. QSqlTableModel and LIKE
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2008, 08:45
  5. QSqlTableModel Help pls
    By munna in forum Newbie
    Replies: 1
    Last Post: 26th January 2006, 07:58

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
  •  
Qt is a trademark of The Qt Company.