Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, April 2008
QSqlQueryModel write record


Message 1 in thread

I need a QSqlQueryModel that cna write data to the db tables so I made a 
sub class starting from QSqlQueryModel (setData it is partially completed):

class QToolQryModel : public QExtSqlQueryModel
{
    bool WriteData(QString asTblName, QString asFieldName, QString 
asIxKey, int iId, const QString &asData)
    {
         QSqlQuery query;
         QString asQry = "update " + asTblName + " set " + asFieldName + 
" = ? where " + asIxKey + " = ?";
                 
         qDebug() << "asQry = " << asQry << ";";
         query.prepare(asQry);
         query.addBindValue(asData);
         query.addBindValue(iId);
         return query.exec();
    }
    
//-----------------------------------------------------------------------------
    bool setData(const QModelIndex &index, const QVariant &value, int role)
    {
        if(false == index.isValid())
            return(false);
        QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
        int id = data(primaryKeyIndex).toInt();
        bool zRet;
        clear();
        switch(index.column())
        {
            case 0:
            case 3: zRet = WriteData("T_Tool", "Description", "idTool", 
id, value.toString());  break;
            case 4: /* zRet = SetFirstName(id, value.toString()); */ break;
            default: qDebug() << "QToolForm id =" << id << " is not valid!";
        }
        if(true == zRet)
            dataChanged(index, index);
        return(zRet);
    }
    
//-----------------------------------------------------------------------------
};   
//------------------------------------------------------------------------------

it is defined as:
    QToolQryModel SqlQryModel;

and initiated as:

void QToolForm::InitMapQry(void)
{
    QModelIndex ModIx;
    SqlQryModel.LoadFromFile("ToolQry.sql");
    if(SqlQryModel.lastError().isValid())
        qDebug() << SqlQryModel.lastError();
    qDebug() << SqlQryModel.query().lastQuery();
    qDebug() << "row count =" << SqlQryModel.rowCount(ModIx);
    // data fields to widget controls mapping
    Map.setModel(&SqlQryModel);
    Map.setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    Map.setItemDelegate(SqlDelegate);
    Map.addMapping(ui.EditToolset, 1);        // T_Tool.idToolSet
    Map.addMapping(ui.EditT, 2);            // T_Tool.T
    Map.addMapping(ui.EditDescription, 3);    // T_Tool.Description
    Map.addMapping(ui.EditCode, 4);            // T_Tool.Code
    Map.toFirst();
    Map.setCurrentIndex(0);
    // view the 1st record
}
//------------------------------------------------------------------------------

when the submit button is pressed this method is executed:

void QToolForm::OnPshBtnSubmitClicked(void)
{
    bool zRet;
   

    zRet = Map.submit();
    qDebug() << "QToolForm::OnPshBtnSubmitClicked() Map.submit() =" << zRet;
/*   
    zRet = SqlTblModel.submitAll();
    qDebug() << "QToolForm::OnPshBtnSubmitClicked() 
SqlTblModel.submitAll() =" << zRet;
*/   
}
//-----------------------------------------------------------------------------

but data on the db is not written.
Where I miss something?
begin:vcard
begin:vcard
fn:Massimo Manca
n:Manca;Massimo
org:Micron Engineering di Massimo Manca
adr:;;via della Ferriera, 48;Pordenone;PN;33170;ITALIA
email;internet:massimo.manca@xxxxxxxxxxxxxxxxxxxx
tel;work:+39 0434 1856131
tel;fax:+39 0434 1851032 / 178 273 3543
tel;cell:+39 349 4504979
url:http://www.micronengineering.it
version:2.1
end:vcard


Message 2 in thread

Does query.lastError() report it's ok if you put if after query.exec() 
and before leaving WriteData ?

--
 [ signature omitted ]