Python 更改模型数据并更新表视图

Python 更改模型数据并更新表视图,python,python-3.x,qt,pyqt5,qtableview,Python,Python 3.x,Qt,Pyqt5,Qtableview,我正在努力让它工作。我希望能得到一些帮助 因此,我有一个tableView,其中有4列是我在程序启动时默认建立的。然后,my main()调用一个函数,该函数创建了一个名为“scanner”的函数线程,该函数扫描串行端口,试图找到arduino发送的特定字符(该结构是为了使它能够拥有多个设备)。如果它找到一个设备,它会将其附加到一个列表中,该列表将包含有效设备连接到的端口,如果没有,它将返回0(“扫描器”在另一个线程上运行时通过队列返回数据)。然后它调用一个函数“reader”,该函数应该为列表

我正在努力让它工作。我希望能得到一些帮助

因此,我有一个tableView,其中有4列是我在程序启动时默认建立的。然后,my main()调用一个函数,该函数创建了一个名为“scanner”的函数线程,该函数扫描串行端口,试图找到arduino发送的特定字符(该结构是为了使它能够拥有多个设备)。如果它找到一个设备,它会将其附加到一个列表中,该列表将包含有效设备连接到的端口,如果没有,它将返回0(“扫描器”在另一个线程上运行时通过队列返回数据)。然后它调用一个函数“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()

我看到您创建了一个self.datatable变量,但从未使用过它。是的,我错了。这是我在这里找到的另一个相关问题中试图解决的问题,但无法实现。如果您意识到在循环中您正在创建一个新模型,而不是将其插入模型中。我建议在循环外创建一次模型,并在循环内使用insertRows方法。你会说西班牙语吗?我把模型放在哪里了?基本上?是的,我会说西班牙语和英语,我想你有一个类,它是在ui中实现的,在这一部分声明模型为类的一个成员,然后你可以通过ui.model进行访问