如何以编程方式更改/更新 Python PyQt4 TableView 中的数据?
Posted
技术标签:
【中文标题】如何以编程方式更改/更新 Python PyQt4 TableView 中的数据?【英文标题】:How to programmatically change/update data in Python PyQt4 TableView? 【发布时间】:2014-11-17 03:45:12 【问题描述】:我想在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】:您需要为此实现Model View Programming。 Python 模型视图编程相当简单。
将模型附加到您的 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_())
【讨论】:
感谢您的回复。我遇到了一些错误,并且能够相应地更新。但是我仍然收到以下错误:TypeError: QAbstractTableModel.beginInsertRows(QModelIndex, int, int): argument 2 has unexpected type '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)) @CM : 我已经更新了代码,看看。要了解我做了什么,请看一下:pyqt.sourceforge.net/Docs/PyQt4/…以上是关于如何以编程方式更改/更新 Python PyQt4 TableView 中的数据?的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式更改 python 装饰器中函数的 argspec?