Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python call parent method from child widget

Tags:

python

pyqt

pyqt4

I am trying to call parent method printName from child widget treeView but Get error like

  1. AttributeError: 'QSplitter' object has no attribute 'printName'
  2. QObject::startTimer: QTimer can only be used with threads started with QThread

why parent is referring to QSplitter ?

Parent of TreeView is supposed to be compositeWidget since TreeViewwas created in 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()
like image 912
django Avatar asked Feb 22 '26 09:02

django


1 Answers

The QTreeView is under QSplitter witch is under compositeWidget. You need to call

self.parent().parent().printName(filePath)
like image 192
djangoliv Avatar answered Feb 23 '26 22:02

djangoliv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!