Results 1 to 3 of 3

Thread: dataChanged fired unexpectedly

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

    Default dataChanged fired unexpectedly

    Good afternoon -

    Had something happen earlier that I can't explain and I was wondering if someone could a) help explain it, and b) help get around it.

    I'm currently capturing a dataChanged() signal on a particular QSqlTablemodel in order to activate a "Save" button. Upon hitting a certain "edit" pushbutton, I'm presented with a dialog allowing me to modify a single field from that model. I hit cancel and the dataChanged() signal is *not* fired, as expected. I again hit "edit" and immediately the signal is fired at the same time the dialog is generated. I didn't expect that to happen and I can't figure out how to prevent that from happening because there's no reason to activate the Save button if I haven't actually changed anything in the model. Here is my setup

    Qt Code:
    1. //****************************************************************************
    2. void MainWindow::RigSetup()
    3. {
    4. //*****************
    5. //* Rig Data... *
    6. //*****************
    7. rigmodel = new QSqlTableModel(this, sqldb->db);
    8.  
    9. rigmodel->setTable ("rig");
    10. rigmodel->sort (1, Qt::AscendingOrder);
    11. rigmodel->setEditStrategy (QSqlTableModel::OnManualSubmit);
    12.  
    13. connect(rigmodel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
    14. this, SLOT(set_save_flag ()));
    15. }
    To copy to clipboard, switch view to plain text mode 

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

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

    I did some checking and it's the "rig name" field that's dirty. I'd really not have to subclass anything to get around this...


    Kodi
    Last edited by mtnbiker66; 25th February 2013 at 18:58.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: dataChanged fired unexpectedly

    I don't see anything wrong with the code, try looking at the QModelIndex row/col values in the dataChanged() signal parameters, that may give a hint to which data has changed.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    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: dataChanged fired unexpectedly

    Use a debugger, set a breakpoint either on dataChanged() or in your code and see where the emission comes from.
    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.


Similar Threads

  1. emitDataChanged and dataChanged signal
    By fruzzo in forum Qt Programming
    Replies: 1
    Last Post: 26th September 2012, 15:47
  2. QSqlTableModel & datachanged()
    By noValue in forum Newbie
    Replies: 5
    Last Post: 26th November 2010, 00:26
  3. Clarification about dataChanged
    By airbites in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 16:02
  4. QAbstractTreeModel & dataChanged()
    By tntcoda in forum Qt Programming
    Replies: 2
    Last Post: 3rd June 2009, 00:12
  5. dataChanged does not re-sort the view
    By alex9099 in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2008, 20: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.