View Full Version : QDataWidgetMapper/QInputDialog oddities...

24th September 2011, 21:31
I super appreciate if anyone can explain something I'm seeing with a combo box and my widget mapper when I call QInputDialog...

I have a QSqlTableModel with values from a row in the database mapped to a combo box. I have a second QsqlTableModel with values mapped to a combo box as well, and it's values are determined by using the first combo box selection as a filter. Image the first CB being a list of baseball teams and clicking on a team causes that team's players to populate this second one; choose a different team in the first and the 2nd changes accordingly.

This works perfectly until the user decides to add a new value to the 2nd combo box by clicking a "New" pushbutton -- I have a clicked() signal calling a slot which calls QInputDialog to prompt for a value...Suddenly, the value in my first combo box resets to the value found at current_index(0) as soon as the dialog is displayed! Just for fun, I checked the index of the first combo box and it's correct before the dialog is displayed, but reset afterwards...I can't find what I'm doing wrong so I don't know what I'm doing wrong...

I commented out the QInputDialog call and just hard coded a value and everything worked fine...

Any ideas? Thanks!


This is the constructor for the first model/combo box

//* Rig Data... *
rigmodel = new QSqlTableModel(this);
rigmodel->setTable ("rig");
rigmodel->sort (1, Qt::AscendingOrder);
rigmodel->setEditStrategy (QSqlTableModel::OnFieldChange);

rigmodel->select ();

record = rigmodel->record (0);
rig_id = record.value ("rig_id").toInt ();

rignamemapper = new QDataWidgetMapper(this);
rignamemapper->setModel (rigmodel);
rignamemapper->toFirst ();
rignamemapper->addMapping (ui->RigInvNameCB, 1);

ui->RigInvNameCB->setModel (rigmodel);
ui->RigInvNameCB->setModelColumn (1);
ui->RigInvNameCB->setCurrentIndex (0);

This is for the second model/combo box

//* Rig Inventory Data... *
riginvmodel = new QSqlTableModel(this);
riginvmodel->setTable ("riginv");
riginvmodel->sort (32, Qt::AscendingOrder);
riginvmodel->setEditStrategy (QSqlTableModel::OnFieldChange);
riginvmodel->setFilter (QString("riginv_rig_id = %1").arg(rig_id));
riginvmodel->select ();

riginvmapper = new QDataWidgetMapper(this);
riginvmapper->setModel (riginvmodel);
riginvmapper->toFirst ();
riginvmapper->addMapping (ui->EquipIDCB, 32);

ui->EquipIDCB->setModel (riginvmodel);
ui->EquipIDCB->setModelColumn (32);
ui->EquipIDCB->setCurrentIndex (0);

The slot where I'm having problems

void MainWindow::on_NewEquipIDPB_clicked()
int row;
bool ok;
int current_rig_id;

QString newequipname;

QSqlRecord record;

qDebug() << "combo index " << ui->RigInvNameCB->currentIndex ();
qDebug() << "mapper index " << rignamemapper->currentIndex ();

//* Go get a name for this piece of equipment... *
newequipname = QInputDialog::getText (this, tr("New Equipment Name"),
tr("Enter a Name for this piece of Equipment"),
QLineEdit::Normal, QString(), &ok);

qDebug() << "combo index " << ui->RigInvNameCB->currentIndex ();
qDebug() << "mapper index " << rignamemapper->currentIndex ();

if (ok && !newequipname.isEmpty ())

Results of the qDebug() calls:

combo index 2
mapper index 2

combo index 0
mapper index -1

26th September 2011, 00:08
And it gets stranger:

1) This issue also happens when my "delete" and "copy" slots are fired as well as clicking in a line edit on my form...
2) This problem only happens once! When it does if I re-select from my first CB it never happens again; I can reselect any value from my first CB and then when I click any other widget it works perfect.
3) I have a slot connected to the first CB this is *not* fired when I'm seeing the value reset itself -- I thought that may be happening but it's not, so somewhere an index is being reset/mapper is losing connection to the CB when another widget is clicked, but again, only once...