我正努力让这个工作起来。我将不胜感激。
所以,我有一个包含4列的tableView,我默认在程序启动时建立。然后,我的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
读者功能:
def reader():
x = 1
rowCount = 0
columnCount = 4
cadena = []
v_headers = []
dispositivos = q.get()
if dispositivos == 0:
print(dispositivos)
cadena = ["NO DEVICE"]
v_headers = cadena
table = [["NO DATA", "NO DATA", "NO DATA", "NO DATA"]]
h_headers = ["Puerto", "Estado", "Versión de Ensamblado", "Versión de Hardware"]
model = RingerTableModel(table, h_headers, v_headers)
ui.tableView.setModel(model)
elif dispositivos:
for device in dispositivos:
print(dispositivos)
rowCount += 1
cadena.append("Dispositivo n°" + " " + str(x))
print(cadena)
x += 1
for y in range(rowCount):
v_headers += cadena
table = [[device[1], "Online", "1.0.0.0", "1.0.0.0"]]
h_headers = ["Port", "Status", "Assembly Version", "Hardware Version"]
model = RingerTableModel(table, h_headers, v_headers)
#ui.tableView.setModel(model)
#model.update(table)
#model.emitDataChanged()
#model.dataChanged()
#ui.tableView.update()