Python 3.x Pyqt5更新列表模型

Python 3.x Pyqt5更新列表模型,python-3.x,qt,qml,pyqt5,Python 3.x,Qt,Qml,Pyqt5,我正在尝试用python更新QML快速控件中的listview模型。此模型中的每个项目都包含地址和状态属性。现在我的代码是: 列表模型: class ServersModel(QAbstractListModel): def __init__(self, parent=None): super(ServersModel, self).__init__(parent) self.list = [] for address, status in

我正在尝试用python更新QML快速控件中的listview模型。此模型中的每个项目都包含
地址
和状态
属性
。现在我的代码是:

列表模型:

class ServersModel(QAbstractListModel):
    def __init__(self, parent=None):
        super(ServersModel, self).__init__(parent)
        self.list = []
        for address, status in (
            ("server 1", "green"),
            ("server 2", "green"),
            ("server 3", "red")):
            self.list.append(server(address, status))

    def rowCount(self, parent=QModelIndex()):
        return len(self.list)

    def data(self, index, role=None):
        if role == Qt.DisplayRole: #show just the name
            person = self.list[index.row()]
            return QVariant(person.name)
        elif role == Qt.UserRole:  #return the whole python object
            person = self.list[index.row()]
            return person
        return QVariant()

    def removeRow(self, position):
        self.list = self.list[:position] + self.list[position+1:]
        self.reset()
服务器对象:

class server(object):
    '''
    a custom data structure, for example purposes
    '''
    def __init__(self, address, status):
        self.address = address
        self.status = status
    def __repr__(self):
        return "%s\n%s"% (self.address, self.status)
Python连接处理程序:

class ServersListViewHandler(QObject):

def __init__(self):
    QObject.__init__(self)

listLoaded = pyqtSignal(QAbstractListModel, arguments=['model'])
data_changed = pyqtSignal(QModelIndex, QModelIndex)

# Slot for detecting clicks in listView
@pyqtSlot(str)
def listViewItemClicked(self, name):
    self.listLoaded.emit(ServersModel())
这是我在qml的连接:

Connections {
        target: serversListViewHandler

        onListLoaded: {
            serversListView.model = model
        }
    }

Address属性应作为值转到文本,状态为圆圈的颜色。若我将模型直接实现到QML,那个么这很好,但我希望动态地更新它并从python加载它。关于如何实现这一点有什么想法吗?我可能完全错了,但是几乎没有关于pyqt 5的教程或参考资料,我目前正在做一个类似的实验。我成功地做到了这一点,为列使用了专用的角色名(在列表视图中)。 对于您的示例,这有点像这样:

class ServersModel(QAbstractListModel):

     AddressRole = Qt.UserRole + 1
     StatusRole = Qt.UserRole + 2

    _roles = {AddressRole: b"address", StatusRole: b"status"}
    ...

    def roleNames(self):
        return self._roles

    def data(self, index, role=None):
        if role == AddressRole: #show just the name
            person = self.list[index.row()]
            return QVariant(person.name)
        elif role == StatusRole:  #return the whole python object
            person = self.list[index.row()]
            return person
        return QVariant()
尽管这可能只针对显示部件,而不是整个更新过程。如果能看到访问模型的QML,我会很感兴趣。
另外,我也不确定是否有这种事情的最佳实践

我目前正在做一个类似的实验。我成功地做到了这一点,为列使用了专用的角色名(在列表视图中)。 对于您的示例,这有点像这样:

class ServersModel(QAbstractListModel):

     AddressRole = Qt.UserRole + 1
     StatusRole = Qt.UserRole + 2

    _roles = {AddressRole: b"address", StatusRole: b"status"}
    ...

    def roleNames(self):
        return self._roles

    def data(self, index, role=None):
        if role == AddressRole: #show just the name
            person = self.list[index.row()]
            return QVariant(person.name)
        elif role == StatusRole:  #return the whole python object
            person = self.list[index.row()]
            return person
        return QVariant()
尽管这可能只针对显示部件,而不是整个更新过程。如果能看到访问模型的QML,我会很感兴趣。 另外,我也不确定是否有这种事情的最佳实践