Segolas
Segolas

Reputation: 575

Qt 4 Sqlite, problem with QDataWidgetMapper

I'm writing a little Symbian app and I'm having troubles with QDataWidgetMapper. This is the code:

void Widget::bindToData(){
    databaseManager = new DatabaseManager();
    dataMapper = new QDataWidgetMapper();
    dataMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    dataMapper->setModel(databaseManager->getTableModel());
    dataMapper->addMapping(ui->debugLabel, databaseManager->getTableModel()->fieldIndex("item")); //ko
    QSqlQuery q;
    bool ret = q.exec("SELECT * FROM expense");

    if (!ret){
        databaseManager->showDebugMsg(q.lastError().text());
    }
    while (q.next()) {
        QString item = q.value(1).toString();
        databaseManager->showDebugMsg(item); //ok
    }

    dataMapper->toFirst();


}

Before I had populate the database with some test row and I'm sure it worked because

databaseManager->showDebugMsg(item);

show me the right data. But the mapping fails. The getTableModel is pretty simple:

QSqlTableModel *  DatabaseManager::getTableModel(){
    tableModel->select();
    return tableModel;
}

and here it is the DatabaseManager constructor:

DatabaseManager::DatabaseManager(){
    debugMsgBox = new QMessageBox();
    this->deleteDB();
    this->openDB();
    this->createExpenseTable();
    this->insertTestExpense("test", 11.0);
    tableModel = new QSqlTableModel();
    tableModel->setTable("expense");

}

Since the insertTestExspense works, I think the problem may be in the setTable()... but I can't see what am I missing...

Any idea?

Upvotes: 1

Views: 905

Answers (1)

serge_gubenko
serge_gubenko

Reputation: 20482

Couple of things you can do to make it work:

1.Check if fieldIndex("item") actually returns a valid field index.

2.For the QLabel mapping also specify text property name for the datamanager's addMapping call:

dataMapper->addMapping(ui->debugLabel, databaseManager->getTableModel()>fieldIndex("item"), "text");

hope this helps, regards

Upvotes: 3

Related Questions