如何在Python PyQt4 TableView中以编程方式更改/更新数据?

时间:2014-11-17 03:45:12

标签: python pyqt4 tableview qwidget

Python Python PyQt4专家:

我希望在QWidget窗口中有一个表,并在每次用户在lineEdit字段中输入值时更新表格。我该如何以编程方式执行此操作?

状态:我的代码出现以下错误:

  

TypeError:QAbstractTableModel.beginInsertRows(QModelIndex,int,int):   参数2具有意外类型'str'

from PyQt4 import QtGui , QtCore
import sys

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s


class ABCTableModel(QtCore.QAbstractTableModel):
    def __init__(self,data,parent=None):
        QtCore.QAbstractTableModel.__init__(self,parent)
        self.__data=data     # Initial Data

    def rowCount( self, parent ):
        return len(self.__data)

    def columnCount( self , parent ):
        return len(self.__data)

    def data ( self , index , role ):
        if role == QtCore.Qt.DisplayRole:
            row = index.row()
            column = index.column()
            value = self.__data[row][column]
            return QtCore.QString(str(value))

    def setData(self, index, value):
        self.__data[index.row()][index.column()] = value
        return True

    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsSelectable      

    def insertRows(self , position , rows , item , parent=QtCore.QModelIndex()):
        # beginInsertRows (self, QModelIndex parent, int first, int last)
        self.beginInsertRows(QtCore.QModelIndex(),position,position)
        self.__data.append(item) # Item must be an array
        self.endInsertRows()
        return True

class Ui_MainWindow(QtGui.QMainWindow , QtGui.QWidget):
    def __init__(self):
        QtGui.QMainWindow.__init__(self,None)
        QtGui.QWidget.__init__(self,None)

    def setupUi(self, MainWindow):
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tableView = QtGui.QTableView(self.centralwidget)
        self.tableModel=ABCTableModel([[1,2,3],[2,3,4],[4,5,6]])
        self.tableView.setModel(self.tableModel) 
        self.lineEdit_1 = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit_2 = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit_3 = QtGui.QLineEdit(self.centralwidget)
        self.tableModel.insertRows(self.lineEdit_1.text(),self.lineEdit_2.text(), self.lineEdit_3.text())

if  __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow() # <-- Instantiate QMainWindow object.
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:3)

您需要为此实施Model View Programming。 Python模型视图编程非常简单。

将一个Model附加到TableView,并在LineEdit中输入并将其插入到模型中,其余的工作将由此处理。

from PyQt4 import QtGui , QtCore
import sys
class ABCTableModel(QtCore.QAbstractTableModel):
    def __init__(self,data,parent=None):
        QtCore.QAbstractTableModel.__init__(self,parent)
        self.__data=data     # Initial Data

    def rowCount( self, parent ):
        return len(self.__data)

    def columnCount( self , parent ):
        return len(self.__data[])

    def data ( self , index , role ):
        if role == QtCore.Qt.DisplayRole:
            row = index.row()
            column = index.column()
            value = self.__data[row][column]
            return QtCore.QString(str(value))

    def setData(self, index, value):
        self.__data[index.row()][index.column()] = value
        return True

    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled|QtCore.Qt.ItemIsEditable|QtCore.Qt.ItemIsSelectable      

    def insertRows(self , position , rows , item , parent=QtCore.QModelIndex()):
        # beginInsertRows (self, QModelIndex parent, int first, int last)
        self.beginInsertRows(QtCore.QModelIndex(),len(self.__data),len(self.__data)+1)
        self.__data.append(item) # Item must be an array
        self.endInsertRows()
        return True

class Ui_MainWindow(QtGui.QMainWindow , QtGui.QWidget):
    def __init__(self):
        QtGui.QMainWindow.__init__(self,None)
        QtGui.QWidget.__init__(self,None)

    def setupUi(self, MainWindow):
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tableView = QtGui.QTableView(self.centralwidget)
        self.tableModel=ABCTableModel([[1,2,3],[2,3,4],[4,5,6]])
        self.tableView.setModel(self.tableModel) 
        self.lineEdit_1 = QtGui.QlineEdit(self.centralwidget)
        self.lineEdit_2 = QtGui.QlineEdit(self.centralwidget)
        self.lineEdit_3 = QtGui.QlineEdit(self.centralwidget)
        self.tableModel.insertRows(self.lineEdit_1.text(),self.lineEdit_2.text(), self.lineEdit_3.text())

if  __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow() # <-- Instantiate QMainWindow object.
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())