[英]How to connect the QFileSystemModel dataChanged signal in PyQt5?
我正在嘗試連接QFileSystemModel.dataChanged
信號,但到目前為止還沒有運氣。 下面的代碼產生此錯誤:
TypeError:預期的字節或ASCII字符串不是'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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.