简体   繁体   English

python从子窗口小部件调用父方法

[英]python call parent method from child widget

I am trying to call parent method printName from child widget treeView but Get error like 我正在尝试从子窗口小部件treeView调用父方法printName ,但得到类似的错误

  1. AttributeError: 'QSplitter' object has no attribute 'printName' AttributeError:“ QSplitter”对象没有属性“ printName”
  2. QObject::startTimer: QTimer can only be used with threads started with QThread QObject :: startTimer:QTimer只能与以QThread开头的线程一起使用

why parent is referring to QSplitter ? 为什么parent是指QSplitter?

Parent of TreeView is supposed to be compositeWidget since TreeView was created in compositeWidget TreeView父级应该是compositeWidget因为TreeView是在compositeWidget中创建的

CODE: 码:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys


class MainExample(QMainWindow):

    def __init__(self, parent=None):
        super(MainExample, self).__init__(parent)
        self.initUI()

    def initUI(self):
        self.mainWidget = compositeWidget(self)
        self.setCentralWidget(self.mainWidget)
        self.mainWidget.treeView.setPath('D:\DATA')
        self.setGeometry(300, 300, 300, 200)


class TreeView(QTreeView):

    def __init__(self, parent):
        super(TreeView, self).__init__(parent)
        self.clicked.connect(self.on_treeView_clicked)

    @pyqtSlot(QModelIndex)
    def on_treeView_clicked(self, index):
        indexItem = self.FileSystemModel.index(index.row(), 0, index.parent())
        filePath = self.FileSystemModel.filePath(indexItem)
        self.parent().printName(filePath)
        #

    def setPath(self, path):
        self.FileSystemModel = QFileSystemModel()
        self.FileSystemModel.setFilter(QDir.Dirs | QDir.NoDotAndDotDot)
        self.FileSystemModel.setRootPath(path)
        self.setModel(self.FileSystemModel)
        index = self.FileSystemModel.index(path)
        self.setRootIndex(index)


class compositeWidget(QWidget):

    def __init__(self, parent):
        super(compositeWidget, self).__init__(parent)
        self.treeView = TreeView(self)
        self.frame = QFrame()
        splitterHorizontal = QSplitter(Qt.Horizontal)
        splitterHorizontal.addWidget(self.treeView)
        splitterHorizontal.addWidget(self.frame)
        splitterHorizontal.setSizes([10, 190])
        self.layout = QHBoxLayout(self)
        self.layout.addWidget(splitterHorizontal)
        self.setLayout(self.layout)

    def printName(self):
        print 'fileName'


def main():

    app = QApplication(sys.argv)
    ex = MainExample()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

The QTreeView is under QSplitter witch is under compositeWidget. QTreeView在QSplitter下,而在CompositeWidget下。 You need to call 你需要打电话

self.parent().parent().printName(filePath)

Unfortunately this doesn't seem to be documented (which seems like a little bit of an oversight) but as with other addWidget() methods in Qt (like QLayout.addWidget() ) the QSplitter.addWidget() method takes ownership of the child by becoming it's parent. 不幸的是,这似乎没有被记录(似乎有点疏忽),但是与Qt中的其他addWidget()方法(例如QLayout.addWidget() )一样, QSplitter.addWidget()方法获得了子项的所有权。通过成为它的父母。

This is why the QSplitter is returned by Treeview.parent() . 这就是为什么Treeview.parent()返回QSplitter原因。 You should use another way to access the parent you want (for instance like explictly storing a reference to the parent you pass into the constructor) 您应该使用另一种方式访问​​所需的父级(例如,像显式存储对传递给构造函数的父级的引用一样)

class TreeView(QTreeView):

    def __init__(self, parent):
        super(TreeView, self).__init__(parent)
        self.clicked.connect(self.on_treeView_clicked)
        self.composite_widget = parent

    @pyqtSlot(QModelIndex)
    def on_treeView_clicked(self, index):
        indexItem = self.FileSystemModel.index(index.row(), 0, index.parent())
        filePath = self.FileSystemModel.filePath(indexItem)
        self.composite_widget.printName(filePath)
        #

    def setPath(self, path):
        self.FileSystemModel = QFileSystemModel()
        self.FileSystemModel.setFilter(QDir.Dirs | QDir.NoDotAndDotDot)
        self.FileSystemModel.setRootPath(path)
        self.setModel(self.FileSystemModel)
        index = self.FileSystemModel.index(path)
        self.setRootIndex(index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM