如何连接PyQt5中的QFileSystemModel dataChanged信号?

问题描述:

我试图连接QFileSystemModel.dataChanged信号,但目前还没有运气。下面的代码是产卵此错误:如何连接PyQt5中的QFileSystemModel dataChanged信号?

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() 

我怎么能在这PyQt5连接信号?

如果没有任何重载,则不需要显式选择信号。因此,连接信号正确的方法是这样的:

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

但在任何情况下,当你做需要选择签名,你必须通过在表示一类两种类型的对象或字符串。在你的特定情况下,字符串必须使用,因为第三个参数没有对应的类型对​​象。因此,上述信号连接的显式版本将为:

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

谢谢!这是一个完美的答案,我根本不理解产生的错误。 – BPL