0

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
ChrisM
  • 1,576
  • 6
  • 18
  • 29
seghier
  • 167
  • 1
  • 2
  • 11
  • Your code is correct and I can not reproduce your problem. – eyllanesc Oct 01 '17 at 23:42
  • thanks ; the problem still happen ; print filePath give me the same name twice everytime ; and dupicated items in the combobox – seghier Oct 01 '17 at 23:47
  • Which combobox? – eyllanesc Oct 01 '17 at 23:52
  • to not have that problem because you do not work with the double click. – eyllanesc Oct 01 '17 at 23:53
  • i check again and the problem from qt designer ; if i put the qtreeview inside widget or QVBoxlayout i got this problem ! – seghier Oct 02 '17 at 00:06
  • and this is not the problem ; something wrong in qt designer ! – seghier Oct 02 '17 at 00:12
  • This happens because of the [Connecting Slots By Name](http://pyqt.sourceforge.net/Docs/PyQt4/new_style_signals_slots.html#connecting-slots-by-name) feature. The pyuic tool can automatically connect signals to slots based on a simple naming convention. To fix your code, you can either use a different name for the slot, or remove the line in your code that connects the signal. – ekhumoro Oct 02 '17 at 00:20
  • problem solved but i don't know why it happened ; i rename treeView to treeview – seghier Oct 02 '17 at 00:23
  • thank you all ; the problem fixed ; ekhumoro is right if he mean to rename treeView – seghier Oct 02 '17 at 00:25

1 Answers1

0

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:

  • Delete the connection you make.
  • Or rename your slot.
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • thank you ; and sorry i can't vote because i am new member – seghier Oct 02 '17 at 00:33
  • @seghier You can not vote but you can mark a response as correct for it must mark the arrow that is to the left side of my answer, to have more information check the following: https://stackoverflow.com/tour – eyllanesc Oct 02 '17 at 00:39
  • @eyllanesc i have the same problem, the more times i click the TreeView the more times it prints the filepath instead of once...pls how can i rename my slot or what better method can i use... – X-Black... May 27 '18 at 22:30
  • @X-Black... Without a code, I can not tell you anything. – eyllanesc May 27 '18 at 22:31
  • ' def on_clicked(self, index): #self.path = self.fileSystemModel.fileInfo(index).absoluteFilePath() self.path = self.fileSystemModel.filePath(index) #print(self.path) #self.temp_delete_button.clicked.connect(self.delete_file) #self.duplicate_delete_button.clicked.connect(self.delete_file) #self.unused_delete_button.clicked.connect(self.delete_file) self.temp_zip_button.clicked.connect(self.zip_file) def zip_file(self,): print(self.path) ' – X-Black... May 27 '18 at 22:37
  • @eyllanesc the print() in the on_clicked() prints once at time but the print in the zip_file() prints repeatedly... – X-Black... May 27 '18 at 22:39
  • The problem is another one, you are connecting the button with the zip_file slot every time, do not make the connection in the on_clicked, but below the creation of the button: `self.temp_zip_button = QPushButton('Zip file')` `self.temp_zip_button.clicked.connect (self.zip_file)` The connection does not verify if the connection with any slot was made previously. – eyllanesc May 27 '18 at 22:40
  • @eyllanesc I actually did it that way before it didn't work that's why i tried manipulating the code...i did it the normal way again..surprisingly it worked..thanks again – X-Black... May 27 '18 at 22:48