Python:PyQt QTreeview 示例 - 选择 [英] Python: PyQt QTreeview example - selection

查看:33
本文介绍了Python:PyQt QTreeview 示例 - 选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 2.7 和 Qt 设计器,而且我是 MVC 的新手:我在 Qt 中完成了一个视图,它为我提供了一个目录树列表,以及用于运行事物的控制器.我的问题是:

I'm using Python 2.7 and Qt designer and I'm new to MVC: I have a View completed within Qt to give me a directory tree list, and the controller in place to run things. My question is:

给定 Qtree 视图,选择目录后如何获取目录?

代码快照如下,我怀疑它是 SIGNAL(..) 虽然我不确定:

Code snap shot is below, I suspect it's SIGNAL(..) though I'm unsure:

class Main(QtGui.QMainWindow):
  plot = pyqtSignal()

  def __init__(self):
    QtGui.QMainWindow.__init__(self)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)

    # create model
    model = QtGui.QFileSystemModel()
    model.setRootPath( QtCore.QDir.currentPath() )

    # set the model
    self.ui.treeView.setModel(model)

    **QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)**

  def test(self):
    print "hello!"

推荐答案

您正在寻找的信号是 selectionChanged 由您的树拥有的 selectionModel 发出.此信号以 selected 项作为第一个参数和 deselected 作为第二个参数发出,两者都是 QItemSelection.

The signal you're looking for is selectionChanged emmited by the selectionModel owned by your tree. This signal is emmited with the selected item as first argument and the deselected as second, both are instances of QItemSelection.

所以你可能想改变这一行:

So you might want to change the line:

QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)

QtCore.QObject.connect(self.ui.treeView.selectionModel(), QtCore.SIGNAL('selectionChanged()'), self.test)

另外,我建议您使用信号和槽的新样式.将您的 test 函数重新定义为:

Also I recommend you to use the new style for signals and slots. Redefine your test function as:

 @QtCore.pyqtSlot("QItemSelection, QItemSelection")
 def test(self, selected, deselected):
     print("hello!")
     print(selected)
     print(deselected)

这里有一个工作示例:

from PyQt4 import QtGui
from PyQt4 import QtCore

class Main(QtGui.QTreeView):

  def __init__(self):

    QtGui.QTreeView.__init__(self)
    model = QtGui.QFileSystemModel()
    model.setRootPath( QtCore.QDir.currentPath() )
    self.setModel(model)
    QtCore.QObject.connect(self.selectionModel(), QtCore.SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.test)

  @QtCore.pyqtSlot("QItemSelection, QItemSelection")
  def test(self, selected, deselected):
      print("hello!")
      print(selected)
      print(deselected)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Main()
    w.show()
    sys.exit(app.exec_())

PyQt5

在 PyQt5 中有点不同(感谢 Carel 和 saldenisov 的评论和回答.)

PyQt5

In PyQt5 is a little bit different (thanks to Carel and saldenisov for comments and aswer.)

... 当 PyQt 从 4 变为 5 时,connect 从对象方法转变为作用于属性的方法

... connect moved from being an object method to a method acting upon the attribute when PyQt went from 4 to 5

所以取而代之的是已知的:

So instead the known:

QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)

现在你写:

class Main(QTreeView):
    def __init__(self):
        # ...  
        self.setModel(model)
        self.doubleClicked.connect(self.test)  # Note that the the signal is now a attribute of the widget.

这是使用 PyQt5 的示例(由 saldenisov).

Here is a the example (by saldenisov) using PyQt5.

from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication

class Main(QTreeView):
    def __init__(self):
        QTreeView.__init__(self)
        model = QFileSystemModel()
        model.setRootPath('C:\\')
        self.setModel(model)
        self.doubleClicked.connect(self.test)

    def test(self, signal):
        file_path=self.model().filePath(signal)
        print(file_path)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Main()
    w.show()
    sys.exit(app.exec_())

这篇关于Python:PyQt QTreeview 示例 - 选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆