【问题标题】:QDataWidgetMapper autosubmit doesn't workQDataWidgetMapper 自动提交不起作用
【发布时间】:2011-02-22 11:31:25
【问题描述】:

我创建了一个 sqldatabase 并用一些测试数据填充它。我正在使用 QDataWidgetMapper,我认为我连接正确,因为我可以使用以下代码读取测试记录:

    databaseManager = new DatabaseManager();
    dataMapper = new QDataWidgetMapper();
    dataMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    dataMapper->setModel(databaseManager->getTableModel());

    // aggiungo una riga nella tabella ui per ogni riga nel database
    for(int i=0; i<databaseManager->getTableModel()->rowCount(); i++){
        this->addRow(); 
    }
   dataMapper->toFirst();

这里是 addRow() 方法(在 QTableWidget 中创建行并将它们映射到模型):

int Widget::addRow(){
    int rowsNum = ui->moneyTableWidget->rowCount();
    ui->moneyTableWidget->insertRow(rowsNum);
    QLineEdit *newQLineEdit = new QLineEdit();
    QLineEdit *newItemLabel = new QLineEdit();

    QDoubleValidator *doubleValidator = new QDoubleValidator(0.0, 5.0, 2, this);
    newQLineEdit->setValidator(doubleValidator);

    QObject::connect(newQLineEdit, SIGNAL(textChanged(const QString &)),
                     this, SLOT(updateTotal()));
    ui->moneyTableWidget->setCellWidget(rowsNum, 0, newItemLabel);
    ui->moneyTableWidget->setCellWidget(rowsNum, 1, newQLineEdit);
       dataMapper->addMapping(newQLineEdit,databaseManager->getTableModel()->fieldIndex("price"), "text");
    dataMapper->addMapping(newItemLabel,databaseManager->getTableModel()->fieldIndex("item"), "text");

    return rowsNum;
}

问题是自动提交不起作用。如果我向 QtableWidget 添加行(使用相同的 addRow 方法),或者如果我修改/删除现有行(带有测试数据的行),则不会发生任何事情。

或者更好的是 gui 反映了更改,但重新运行应用程序我得到了与以前相同的测试日期。

有什么想法吗?有没有办法测试数据库发生了什么?如果它是一个普通的查询 q,我会做一个 q.lastError()。

谢谢

【问题讨论】:

    标签: c++ sqlite qt4 symbian


    【解决方案1】:

    当您为 QDataWidgetMapper 设置提交策略时,它定义了数据如何从小部件传输到模型的相应字段的方式。每当小部件失去焦点时,AutoSubmit 都会将数据传输到模型。如果为模型的dataChanged 信号定义处理程序,您应该能够跟踪模型的更改。

    现在,根据您的描述,您似乎希望在重新启动应用时模型中会出现一个新行。为了做到这一点,数据模型应该通过手动(QSqlTableModel::submitAll)调用或在字段或行更改时自动提交更改。为了为您的模型设置编辑策略,请使用 QSqlTableModel::setEditStrategy 和可能的值之一:

    QSqlTableModel::OnFieldChange - All changes to the model will be applied immediately to the database.
    QSqlTableModel::OnRowChange - Changes to a row will be applied when the user selects a different row.
    QSqlTableModel::OnManualSubmit - All changes will be cached in the model until either submitAll() or revertAll() is called.
    

    希望这会有所帮助,问候

    【讨论】:

    • 它开始起作用了。现在,当我更新小部件中的一行时,将对模型中的 all 行进行更改...但我认为这是我的代码中的一个错误:-)。再次感谢! (我认为 qt 没有很好的文档记录)
    猜你喜欢
    • 2011-04-14
    • 1970-01-01
    • 2017-11-05
    • 2011-09-25
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多