I have a problem with QFileSystemModel.index
When I select files or folders from treeview with one click of the mouse, the code prints the item selected twice.
This is the part of the code where I am having the problem :
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
The problem is caused by the following line in the file generated when compiling Qt Designer.
QtCore.QMetaObject.connectSlotsByName(SOME_OBJECT)
According to the 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();
that is to say that it connects the slots and the signals that contain that syntax, and in your case that happens so you have 2 options: