我正努力让这个工作起来。我会很感激你的帮助。在
因此,我有一个表视图,其中有4列,我在程序启动时默认建立。然后,我的main()调用一个函数,该函数创建一个名为“scanner”的函数的线程,该函数扫描串行端口,试图找到arduino发送的特定字符(该结构使得它可以拥有多个设备)。如果找到一个设备,它会将它附加到一个列表中,该列表将包含一个有效设备连接到的端口,如果没有,则返回0(“scanner”在另一个线程上运行时通过队列返回数据)。然后它调用了一个函数“reader”,该函数应该在列表中为每个设备添加一行到TableView,其中包含有关设备的数据,但是我对Qt还不熟悉,似乎无法理解它。下面是一些代码:
模型等级:class RingerTableModel(QAbstractTableModel):
def __init__(self, data=[[]], horizontalHeaders=[], verticalHeaders=[], parent=None):
QtCore.QAbstractTableModel.__init__(self, parent)
self.__data = data
self.__horizontalHeaders = horizontalHeaders
self.__verticalHeaders = verticalHeaders
def rowCount(self, parent):
return len(self.__data)
def columnCount(self, parent):
return len(self.__data[0])
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsSelectable
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
value = self.__data[row][column]
return value
def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return self.__horizontalHeaders[section]
if orientation == QtCore.Qt.Vertical:
return self.__verticalHeaders[section]
else:
pass
def update(self, dataIn):
print('Updating Model')
self.datatable = dataIn
print('Datatable : {0}'.format(self.datatable))
def emitDataChanged(self):
self.dataChanged.emit(QModelIndex(), QModelIndex())
# =====================================================#
# INSERTING & REMOVING
# =====================================================#
def insertRows(self, position, rows, parent=QModelIndex()):
self.beginInsertRows(parent, position, position + rows - 1)
for i in range(rows):
#defaultValues = ["prueba" for i in range(self.columnCount(None))]
self.__data.insert(position, "hola")
self.endInsertRows()
return True
def insertColumns(self, position, columns, parent=QModelIndex()):
self.beginInsertColumns(parent, position, position + columns - 1)
rowCount = len(self.__data)
for i in range(columns):
for j in range(rowCount):
self.__data[j].insert(position, QtGui.QColor("#000000"))
self.endInsertColumns()
return True
读卡器功能:
^{pr2}$