Results 1 to 8 of 8

Thread: dataChanged, isDirty -- how to tell data was changed...

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

    Default dataChanged, isDirty -- how to tell data was changed...

    Afternoon all --

    Done a lot of searching, high and low, and experimenting but I can't seem to hit on a solution to what I'm trying to do...Here's what I have...

    QSqlTableModel on a SQlite table with 29 columns...
    29 Line edits mapped to these columns (one of them to a combobox)
    editStrategy = OnManualSubmit
    Request that any change to the data enables a "save" push button, a la SQlite Database Brower (if you've used that)...Basically, the save button doesn't enable until a change to the data has occurred...

    So I tried a slot that is fired on a dataChanged signal, but I found that tabbing from line edit to line edit fired it -- is that expected behavior? If not -- I know this can't be that hard -- any suggestions to pull off what I'm trying to do? Basically I want to inform the user that they have changes they need to commit if they try to navigate to another place in the app...

    Just when I think I've learned a lot about Qt in the past 9 months (despite my first child's birth, colic and reflux getting in the way), what looks to be obvious and easy implementations humble me...


    scott

  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: dataChanged, isDirty -- how to tell data was changed...

    The dataChanged() signal should not be emitted until you submitAll() or revertAll() in the manual submit mode (or another view changes the data through the model) so I don't know how exactly you get the behaviour you describe. Are you using a table view or a QDataWidgetMapper? Single view or multiple?

    QSqlTableModel::isDirty() can tell you if a given index is dirty or not. If you look for currentChanged() from the view's selection model then check the old index you might be part way there.

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

    Default Re: dataChanged, isDirty -- how to tell data was changed...

    @ChrisW67

    I'm doing things very vanilla...Creating a QSqlTableModel to a table with the edit strategy of OnManualSubmit, then the I create a mapper to the lineedits that reflect the data...It's weird -- I didn't expect the slot to be fired just by tabbing from widget to widget...

    Qt Code:
    1. //***************************
    2. //* Rig Inventory Data... *
    3. //***************************
    4. riginvmodel = new QSqlTableModel(this);
    5. riginvmodel->setTable ("rig_inventory");
    6. riginvmodel->sort (32, Qt::AscendingOrder);
    7. riginvmodel->setEditStrategy (QSqlTableModel::OnManualSubmit);
    8.  
    9. riginvmodel->setFilter (QString("riginv_rig_id = %1").arg(rig_id));
    10. riginvmodel->select ();
    11.  
    12. riginvmapper = new QDataWidgetMapper(this);
    13. riginvmapper->setModel (riginvmodel);
    14.  
    15. ui->EquipIDCB->setModel (riginvmodel);
    16. ui->EquipIDCB->setModelColumn (32);
    17. ui->EquipIDCB->setCurrentIndex (0);
    18.  
    19. riginvmapper->addMapping (ui->RigInvSerialNumLE, 0);
    20. riginvmapper->addMapping (ui->RigInvNumJointsLE, 1);
    21. riginvmapper->addMapping (ui->RigInvTypeLE, 2);
    22. riginvmapper->addMapping (ui->RigInvWtLE, 3);
    23. riginvmapper->addMapping (ui->RigInvJointLenLE, 4);
    24. riginvmapper->addMapping (ui->RigInvODLE, 5);
    25. .
    26. .
    27. .
    28. riginvmapper->addMapping (ui->EquipIDCB, 32);
    29.  
    30.  
    31. riginvmapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    32. riginvmapper->toFirst ();
    33. .
    34. .
    35. .
    36. connect(riginvmodel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
    37. this, SLOT(enable_save_button()));
    To copy to clipboard, switch view to plain text mode 

    And weirder -- a tabe between line edits results in my SLOT enable_save_button() because called twice!

    As for the "isDirty" option -- I was hoping to have a flag I could check based on the model overall, not by index, but I guess if I could get the dataChanged(...) working then I could have it set a flag/enable the "save" button"...

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

    Default Re: dataChanged, isDirty -- how to tell data was changed...

    This is getting really frustrating...Even with no changes to my data, tabbing thru the line edits not only causes the dataChanged() signal to fire, but when that happened I checked the see if the index was dirty, and sure enough it was! And doing a submitALL doesn't change that...


    scott

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: dataChanged, isDirty -- how to tell data was changed...

    I don't think you can use the QSqlTableModel::dataChanged() signal with a QDataWidgetMapper and accomplish what you want. From the QDataWidgetMapper docs here your choices are AutoSubmit, which triggers the dataChanged signal when you tab through your lineedits or ManualSubmit which won't produce a signal.

    Looks to me like you need to connect the signal QLineEdit::textEdited(QString) for each lineedit to a slot that enables your "save" button.

  6. The following user says thank you to norobro for this useful post:

    scott_hollen (1st October 2011)

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

    Default Re: dataChanged, isDirty -- how to tell data was changed...

    Thanks for the reply -- I got tired of trying to make this work late last night and that's the exact way I was getting around it...Pain in butt because I have so many fields but you gotta make the client happy, right?

    And I do have my mapper set to AutoSubmit but I didn't think to look up the signal that gets fired due to that...

    thanks again!


    scott
    Last edited by scott_hollen; 1st October 2011 at 16:16.

  8. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: dataChanged, isDirty -- how to tell data was changed...

    Quote Originally Posted by scott_hollen View Post
    ...Pain in butt because I have so many fields ...
    Two lines of code should do it if all of your QLineEdits are to be connected:
    Qt Code:
    1. foreach(QLineEdit *lineEdit, findChildren<QLineEdit *>())
    2. connect(lineEdit,SIGNAL(textEdited(QString)),this,SLOT(enable_save_button()()));
    To copy to clipboard, switch view to plain text mode 
    Or, you can subclass QLineEdit, put the connect statement in the ctor and promote the lineEdits you want connected in the designer.

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

    Default Re: dataChanged, isDirty -- how to tell data was changed...

    Whoa, that saves me a LOT of time -- THANKS!!!! I need to be doing this more than as a side project to pick up all that I'm missing as a part-timer...

    scot

Similar Threads

  1. Replies: 2
    Last Post: 7th July 2011, 09:16
  2. Replies: 6
    Last Post: 25th June 2011, 12:35
  3. Replies: 12
    Last Post: 24th July 2010, 09:12
  4. Access to QSqlTableModel::isDirty Question.
    By patrik08 in forum Qt Programming
    Replies: 3
    Last Post: 12th April 2007, 18:49
  5. Informing a model its data has changed
    By saknopper in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 20: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.