c++ - Qt 4 Sqlite,QDataWidgetMapper 问题

标签 c++ sqlite qt4

我正在编写一个 Symbian 小应用程序,但在使用 QDataWidgetMapper 时遇到了问题。这是代码:

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();


}

在我用一些测试行填充数据库之前,我确信它有效,因为

databaseManager->showDebugMsg(item);

显示正确的数据。但是映射失败。 getTableModel 非常简单:

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

这里是 DatabaseManager 的构造函数:

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

}

由于 insertTestExspense 有效,我认为问题可能出在 setTable() 中......但我看不出我错过了什么......

有什么想法吗?

最佳答案

您可以做几件事来让它发挥作用:

1.检查 fieldIndex("item") 是否实际返回有效的字段索引。

2.对于QLabel映射也指定text数据管理器的 addMapping 调用的属性名称:

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

希望这对你有帮助,问候

关于c++ - Qt 4 Sqlite,QDataWidgetMapper 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5030082/

相关文章:

c++ - add_library 没有使用 c 源代码创建 .lib

c++ - 用户自定义bool转换的优先级

c++ - boost spirit 还原解析

Linux 薄荷 : using qt creator with qt4. 6.3

c++ - makefile 以及针对 Linux 程序和 C++ 使用 eclipse

Android - 无法打开 [SQL] 数据库错误

ios - 执行插入或更新 2 个表 SQLITE 的有效方法

android - 为什么我会收到错误 "table is not created?"有什么问题?

python - 在 TreeView PyQt4 中单击时无法抓取项目?

qt - 当 QGraphicsScene 中宽度减小到 200 以下时,x,y 点会发生移动