PDA

View Full Version : dataChanged fired unexpectedly



mtnbiker66
25th February 2013, 18:52
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



//************************************************** **************************
void MainWindow::RigSetup()
{
//*****************
//* Rig Data... *
//*****************
rigmodel = new QSqlTableModel(this, sqldb->db);

rigmodel->setTable ("rig");
rigmodel->sort (1, Qt::AscendingOrder);
rigmodel->setEditStrategy (QSqlTableModel::OnManualSubmit);

connect(rigmodel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(set_save_flag ()));
}




//************************************************** **************************
void MainWindow::on_EditRigAdminPB_clicked()
{
bool ok;
int current_index;

QString oldrig_name;
QString newrig_name;
QString message;
QSqlRecord rig_record;

current_index = ui->RigAdminNameCB->currentIndex ();
rig_record = rigmodel->record (current_index);
oldrig_name = rig_record.value("rig_name").toString();

//***********************************
//* Go get a name for this rig... *
//***********************************
ok = false;
message = "Enter a New Name for this Rig: " + oldrig_name;
newrig_name = QInputDialog::getText (this, "Rig Name Change",
message, QLineEdit::Normal, QString(), &ok);

if (ok && !newrig_name.isEmpty ())
{
if (!rig_mgr)
rig_mgr = new RigManager;

if (rig_mgr->dup_rig_name_check (rigmodel, newrig_name))
{
QMessageBox::warning (this, "Rig Name exists",
"A Rig data set of this name "
"already exists. Please re-enter.");
}
else
{
//* Copy the data into the model... *
rig_mgr->edit_rig(rigmodel, user, newrig_name, current_index)

}
}
}




//************************************************** **************************
void MainWindow::set_save_flag ()
{
ui->actionSave_All->setEnabled(true);
}


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

Santosh Reddy
25th February 2013, 21:05
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.

wysota
25th February 2013, 21:52
Use a debugger, set a breakpoint either on dataChanged() or in your code and see where the emission comes from.