Good evening boys,
happy holiday! I'm using a QDataWidgetMapper object to map any control of a form to my db table, this is the code:
mapper->setModel(mdlLav);
mapper->addMapping(ui->lblTipoStamp,mdlLav->fieldIndex("TipoStamp"));
mapper->addMapping(ui->idOrdine, mdlLav->fieldIndex("CodOrd"));
mapper->addMapping(ui->spCapi,mdlLav->fieldIndex("QtaCapi"));
mapper->addMapping(ui->dateTimeEdit, mdlLav->fieldIndex("Data"));
mapper->addMapping(ui->spMetri,mdlLav->fieldIndex("Metri"));
mapper->addMapping(ui->spGrandi, mdlLav->fieldIndex("QtaGrandiO"));
mapper->addMapping(ui->spPiccole, mdlLav->fieldIndex("QtaPiccoleO"));
mapper->addMapping(ui->txtIDBolla,mdlLav->fieldIndex("IDBolla"));
mapper->addMapping(ui->cboTipoSpec,typeIndex);
mapper->addMapping(ui->cboColore,typeIndex2);
mapper->addMapping(ui->cboSpessore,typeIndex3);
this->idOrd=idOrd;
if (idLav==-1){
mdlLav->insertRow(mdlLav->rowCount());
ui->lblTipoStamp->setText("#-@-*");
mapper->toLast();
Pulisci();
}
else{
for (int i=0; i<mdlLav->rowCount(); i++){
int IdRec= mdlLav->record(i).field("IDLav").value().toInt();
if (IdRec==idLav){
mapper->setCurrentIndex(i);
break;
}
}
}
mapper = new QDataWidgetMapper(this);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->setModel(mdlLav);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
mapper->addMapping(ui->lblTipoStamp,mdlLav->fieldIndex("TipoStamp"));
mapper->addMapping(ui->idOrdine, mdlLav->fieldIndex("CodOrd"));
mapper->addMapping(ui->spCapi,mdlLav->fieldIndex("QtaCapi"));
mapper->addMapping(ui->dateTimeEdit, mdlLav->fieldIndex("Data"));
mapper->addMapping(ui->spMetri,mdlLav->fieldIndex("Metri"));
mapper->addMapping(ui->spGrandi, mdlLav->fieldIndex("QtaGrandiO"));
mapper->addMapping(ui->spPiccole, mdlLav->fieldIndex("QtaPiccoleO"));
mapper->addMapping(ui->txtIDBolla,mdlLav->fieldIndex("IDBolla"));
mapper->addMapping(ui->cboTipoSpec,typeIndex);
mapper->addMapping(ui->cboColore,typeIndex2);
mapper->addMapping(ui->cboSpessore,typeIndex3);
this->idOrd=idOrd;
if (idLav==-1){
mdlLav->insertRow(mdlLav->rowCount());
ui->lblTipoStamp->setText("#-@-*");
mapper->toLast();
Pulisci();
}
else{
for (int i=0; i<mdlLav->rowCount(); i++){
int IdRec= mdlLav->record(i).field("IDLav").value().toInt();
if (IdRec==idLav){
mapper->setCurrentIndex(i);
break;
}
}
}
To copy to clipboard, switch view to plain text mode
The problem is, the new insert of an element work OK, the Edit of an existing element is KO! I check all the field of db and all data are ok, I think it could be near the combobox indexing.
I use connect to check the value selected in combobox to one table, with this function:
void rtcLavoraOrd::CheckTipoStamp(){
ui->cmdConferma->setEnabled(false);
if (!ui->cboColore->currentText().isEmpty() && !ui->cboSpessore->currentText().isEmpty() && !ui->cboTipoSpec->currentText().isEmpty()){
//Verifica della presenza della combinazione scelta
qry.exec("SELECT ID FROM TIPISTAMP");
bool found=false;
while(qry.next()){
if (qry.value(0).toString()==ui->lblTipoStamp->text()){
found=true;
break;
}
}
if (!found){
ui->fraAlarm->setVisible(true);
ui->txtMaxGrandi->setText("(disp.: ###)");
ui->txtMaxPiccole->setText("(disp.: ###)");
}
else {
colore=ui->cboColore->currentText();
tipo=ui->cboTipoSpec->currentText();
spess=ui->cboSpessore->currentText();
qry2.clear();
qry2
=QString("%1%2").
arg(totStampateQuery
).
arg(QString(" WHERE STAMP.Colore='%1' AND STAMP.SPESSORE=%2 AND STAMP.TIPO='%3'").
arg(colore
).
arg(spess
).
arg(tipo
));
int maxG=-1,maxP=-1;
query.clear();
query.exec(qry2);
while (query.next()){
maxG = query.value(3).toInt();
maxP = query.value(4).toInt();
}
//Calcolo dei valori di grandi e piccole
ui
->txtMaxGrandi
->setText
(QString("(disp.: %1)").
arg(maxG
));
ui
->txtMaxPiccole
->setText
(QString("(disp.: %1)").
arg(maxP
));
ui->cmdConferma->setEnabled(true);
}
}
}
void rtcLavoraOrd::CheckTipoStamp(){
ui->cmdConferma->setEnabled(false);
if (!ui->cboColore->currentText().isEmpty() && !ui->cboSpessore->currentText().isEmpty() && !ui->cboTipoSpec->currentText().isEmpty()){
//Verifica della presenza della combinazione scelta
QSqlQuery qry;
qry.exec("SELECT ID FROM TIPISTAMP");
bool found=false;
while(qry.next()){
if (qry.value(0).toString()==ui->lblTipoStamp->text()){
found=true;
break;
}
}
if (!found){
ui->fraAlarm->setVisible(true);
ui->txtMaxGrandi->setText("(disp.: ###)");
ui->txtMaxPiccole->setText("(disp.: ###)");
}
else {
QString qry2,colore,tipo,spess;
colore=ui->cboColore->currentText();
tipo=ui->cboTipoSpec->currentText();
spess=ui->cboSpessore->currentText();
qry2.clear();
qry2=QString("%1%2").arg(totStampateQuery).arg(QString(" WHERE STAMP.Colore='%1' AND STAMP.SPESSORE=%2 AND STAMP.TIPO='%3'").arg(colore).arg(spess).arg(tipo));
int maxG=-1,maxP=-1;
query.clear();
query.exec(qry2);
while (query.next()){
maxG = query.value(3).toInt();
maxP = query.value(4).toInt();
}
//Calcolo dei valori di grandi e piccole
ui->txtMaxGrandi->setText(QString("(disp.: %1)").arg(maxG));
ui->txtMaxPiccole->setText(QString("(disp.: %1)").arg(maxP));
ui->cmdConferma->setEnabled(true);
}
}
}
To copy to clipboard, switch view to plain text mode
It disable the Save button, but not change the mapper.
Any idea?
Thanks for your time.
Michele
Bookmarks