如何以编程方式在QtableView的新行中插入值

时间:2017-12-01 14:15:39

标签: c++ qt qtableview qsqlquery qsqltablemodel

我正在使用QtableView来显示和编辑来自QsqlTableModel的数据。 一切都很好:显示来自postgreSQL表的数据,用户可以编辑它并保存修改。

我想在点击按钮时添加一行。我使用QslTableModel中的insertRecord方法。 该行已在QTableView中正确添加。

我的问题是: 我想在此新行的第一个单元格中插入查询中的值。 (自动填充唯一标识符)。

这是我的代码:

Selection: 0
There were 18 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In plot.window(c(946944000, 1333324800), c(0.000429087998147139,  ... :
  relative range of values =  37 * EPS, is small (axis 2)
2: In plot.window(c(946944000, 1333324800), c(0.000429087998147139,  ... :
  relative range of values =  32 * EPS, is small (axis 2)
3: In plot.window(c(946944000, 1333324800), c(0.000429087998147139,  ... :
  relative range of values =  32 * EPS, is small (axis 2)
4: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  37 * EPS, is small (axis 2)
5: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)
6: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)
7: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  37 * EPS, is small (axis 2)
8: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)
9: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)
10: In plot.window(c(946944000, 1333324800), c(0.000429087998147139,  ... :
  relative range of values =  32 * EPS, is small (axis 2)
11: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  37 * EPS, is small (axis 2)
12: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)
13: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)
14: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)
15: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  37 * EPS, is small (axis 2)
16: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)
17: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)
18: In doTryCatch(return(expr), name, parentenv, handler) :
  relative range of values =  32 * EPS, is small (axis 2)

该值未插入新行(但如果我手动输入值,则效果很好)。 不过,查询还可以(我可以看到«print identifiant»行,它会返回预期的整数)。

你看到我做错了吗?

是否还有其他方法可以在QTableView单元格中以编程方式插入值?

或者我必须使用QitemDelegate吗?

感谢您提前。

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案:

此行创建一条记录,但不是我的QsqlTableModel

的记录
    record = QtSql.QSqlRecord()

我用这个替换它,它完美地运作:

    record = self.model.record()