重新实现QSqlTableModel.setQuery

时间:2013-07-26 10:28:33

标签: python pyqt pyqt4

我已经重新实现了QSqlTableModel.select()以显示模型在视图中返回的所有结果:

def select(self):

    self.setTable("videos")

    results = QtSql.QSqlTableModel.select(self)

    self.setHeaderData(5, QtCore.Qt.Horizontal, "", QtCore.Qt.DisplayRole)
    self.setHeaderData(6, QtCore.Qt.Horizontal, "Modification")
    self.setHeaderData(9, QtCore.Qt.Horizontal, "Ajout")

    while self.canFetchMore():
        self.fetchMore()
    return results

我想用函数setQuey做同样的事情,所以我尝试了类似的东西:

def setQuery(self, query):


    results = QtSql.QSqlTableModel.setQuery(query)
    self.select(results)

    while self.canFetchMore():
        self.fetchMore()
    return results

但我得到了这个例外:

File "/home/djipey/informatique/python/bibli/model.py", line 23, in setQuery
    results = QtSql.QSqlTableModel.setQuery(query)
    TypeError: QSqlTableModel.setQuery(QSqlQuery): first argument of unbound method must have type 'QSqlTableModel'

你能解释一下为什么我有一个typeError吗?我真的不明白。

1 个答案:

答案 0 :(得分:1)

正如您的覆盖需要两个参数一样,

def setQuery(self, query):

QtSql.QSqlTableModel.setQuery的调用需要两个参数:

results = QtSql.QSqlTableModel.setQuery(self, query)