【问题标题】:PyQt4 QFileSystemModel duplicated indexPyQt4 QFileSystemModel 重复索引
【发布时间】:2017-10-01 23:23:15
【问题描述】:

我对 QFileSystemModel.index 有疑问

当我通过单击鼠标从树视图中选择文件或文件夹时,代码会打印两次选择的项目。

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

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

【问题讨论】:

  • 您的代码是正确的,我无法重现您的问题。
  • 谢谢;问题仍然存在; print filePath 每次都给我两次相同的名字;和组合框中的重复项
  • 哪个组合框?
  • 没有这个问题,因为你不使用双击。
  • 我再次检查,问题来自 qt 设计师;如果我将 qtreeview 放在小部件或 QVBoxlayout 中,我遇到了这个问题!

标签: python-2.7 pyqt4 qtreeview qfilesystemmodel


【解决方案1】:

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

QtCore.QMetaObject.connectSlotsByName(SOME_OBJECT)

根据docs

QMetaObject.connectSlotsByName (QObject o)

递归搜索给定对象的所有子对象,并且 将来自它们的匹配信号连接到跟随的对象槽 如下形式:

void on_<object name>_<signal name>(<signal parameters>);让我们 假设我们的对象有一个 QPushButton 类型的子对象,其中 对象名称按钮 1。捕获按钮的 clicked() 信号的槽 应该是:

void on_button1_clicked();

也就是说,它连接了包含该语法的插槽和信号,在你的情况下,你有两个选择:

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

【讨论】:

  • 谢谢;对不起,我不能投票,因为我是新成员
  • @seghier 您不能投票,但您可以将回复标记为正确,因为它必须标记我的答案左侧的箭头,以获得更多信息,请检查以下内容:stackoverflow.com/tour
  • @eyllanesc 我有同样的问题,我点击 TreeView 的次数越多,它打印文件路径的次数就越多,而不是一次......请问我该如何重命名我的插槽或我可以使用什么更好的方法...
  • @X-Black...没有密码,我什么也不能告诉你。
  • ' 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) '
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
相关资源
最近更新 更多