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

时间:2015-10-28 08:29:27

标签: python pyqt pyqt4

我正在尝试从子窗口小部件printName调用父方法treeView 但得到像

这样的错误
  1. AttributeError:' QSplitter'对象没有属性' printName'
  2. QObject :: startTimer:QTimer只能用于以QThread启动的线程
  3. 为什么父母指的是QSplitter?

    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()
    

2 个答案:

答案 0 :(得分:4)

QTreeView位于QSplitter下,是在compositeWidget下。 你需要打电话

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

答案 1 :(得分:2)

不幸的是,这似乎没有记录(这似乎有点疏忽),但与Qt中的其他QLayout.addWidget()方法一样(如QSplitter.addWidget()QSplitter方法通过成为孩子的父母来取得孩子的所有权。

这就是Treeview.parent()返回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) 的原因。您应该使用另一种方式来访问所需的父级(例如,显式存储对传递给构造函数的父级的引用)

filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential(uri.ToString(), username, pass);