PyQt4 QFileSystemModel 重复索引

PyQt4 QFileSystemModel duplicated index

我有问题 QFileSystemModel.index

当我单击鼠标 select 树视图中的文件或文件夹时,代码会打印项目 selected 两次。

这是我遇到问题的代码部分:

import sys
import os
import sip
sip.setapi('QVariant',2)
.....
.....
self.pathRoot = QtCore.QDir.rootPath()
self.model = QtGui.QFileSystemModel(self)
self.model.setRootPath(self.pathRoot)

self.fsindex = self.model.setRootPath(self.model.myComputer())
self.treeView.setModel(self.model)
self.treeView.setRootIndex(self.fsindex)
self.treeView.clicked.connect(self.on_treeView_clicked)
self.treeView.setColumnHidden(3, True)
self.treeView.setColumnHidden(2, True)
self.treeView.setColumnWidth(0, 320)
self.treeView.setColumnWidth(1, 30)
self.treeView.resizeColumnToContents(True)

@QtCore.pyqtSlot(QtCore.QModelIndex)
def on_treeView_clicked(self, index):
    indexItem = self.model.index(index.row(), 0, index.parent())
    filePath = self.model.filePath(indexItem)
    print filePath

问题是由编译Qt Designer时生成的文件中的以下行引起的。

QtCore.QMetaObject.connectSlotsByName(SOME_OBJECT)

根据 docs:

QMetaObject.connectSlotsByName (QObject o)

Searches recursively for all child objects of the given object, and connects matching signals from them to slots of object that follow the following form:

void on_<object name>_<signal name>(<signal parameters>); Let's assume our object has a child object of type QPushButton with the object name button1. The slot to catch the button's clicked() signal would be:

void on_button1_clicked();

也就是说它连接了包含该语法的插槽和信号,在您的情况下发生了这种情况,因此您有 2 个选项:

  • 删除您建立的连接。
  • 或重命名您的广告位。