BPL
BPL

Reputation: 9863

How to connect the QFileSystemModel dataChanged signal in PyQt5?

I'm trying to connect the QFileSystemModel.dataChanged signal, but with no luck so far. The code below is spawning this error:

TypeError: bytes or ASCII string expected not 'list'

import sys

from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtWidgets import QFileSystemModel, QTreeView
from PyQt5.QtCore import QDir

class DirectoryTreeWidget(QTreeView):

    def __init__(self, path=QDir.currentPath(), *args, **kwargs):
        super(DirectoryTreeWidget, self).__init__(*args, **kwargs)

        self.model = QFileSystemModel()
        self.model.dataChanged[QtCore.QModelIndex,QtCore.QModelIndex,[]].connect(self.dataChanged)

    def dataChanged(self, topLeft, bottomRight, roles):
        print('dataChanged', topLeft, bottomRight, roles)


def main():
    app = QtWidgets.QApplication(sys.argv)
    ex = DirectoryTreeWidget()
    ex.set_extensions(["*.txt"])

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

How can i connect this signal in PyQt5?

Upvotes: 1

Views: 1251

Answers (1)

ekhumoro
ekhumoro

Reputation: 120638

You don't need to explicitly select the signal if doesn't have any overloads. So the correct way to connect the signal is like this:

    self.model.dataChanged.connect(self.dataChanged)

But in any case, when you do need to select the signature, you must pass in either type objects or strings that represent a type. In your particular case, a string must be used, because the third parameter does not have a corresponding type object. So the explicit version of the above signal connection would be:

    self.model.dataChanged[QtCore.QModelIndex, QtCore.QModelIndex, "QVector<int>"].connect(self.dataChanged)

Upvotes: 4

Related Questions