internalPointer方法的工作原理

时间:2015-01-09 22:22:10

标签: python qt model pyqt

在下面的代码中,Car()Plane()都继承自Base类。

enter image description here

每次点击QTableView OnClick()方法都会收到QModelIndex作为其传入参数。

OnClick()方法node=index.internalPointer()行的范围内返回模型self.items变量中定义的Car或Plane实例。

模型self.items不是列表,而是子类分层变量。然而,internalPointer()方法确实使它看起来像索引一个列表变量,并将其作为参数提供给被点击的QModelIndex的行号。

如果您提供有关internalPointer()方法如何工作的更多详细信息,我将不胜感激,因此我可以相应地设计Plane,Car和Base类(因此它们返回我想要的而不是internalPointer()

from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)

class Base(object):    
    def __init__(self, name, parentNode=None):        
        self._name = name
        self._children=[]
        self._parentNode=parentNode
        if parentNode is not None:
            parentNode.addChild(self)
    def typeInfo(self):
        return "BaseNode"
    def addChild(self, child):
        self._children.append(child)
    def name(self):
        return self._name
    def setName(self, name):
        self._name = name
    def child(self, row):
        return self._children[row]
    def childCount(self):
        return len(self._children)
    def getParent(self):
        return self._parentNode
    def row(self):
        if self._parentNode is not None:
            return self._parentNode._children.index(self)

class Car(Base):    
    def __init__(self, name, parent=None):
        super(Car, self).__init__(name, parent)        
    def typeInfo(self):
        return "CarNode"
class Plane(Base):    
    def __init__(self, name, parent=None):
        super(Plane, self).__init__(name, parent)        
    def typeInfo(self):
        return "PlaneNode"

class DataModel(QtCore.QAbstractItemModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)  

        self.items = Base("Base")
        car0 = Car("Car0",  self.items)
        car1 = Car("Car1",  car0)
        car2 = Car("Car2",  car1)

        plane0 = Plane("Plane0",  self.items)
        plane1 = Plane("Plane1",  plane0)
        plane2 = Plane("Plane2",  plane1) 

    def columnCount(self, index=QtCore.QModelIndex()):
        return 3

    def getNodeFromIndex(self, index):    
        if index.isValid():
            node = index.internalPointer()
            if node:
                return node            
        return self.items

    def parent(self, index):
        node = self.getNodeFromIndex(index)
        parentNode = node.getParent()
        if parentNode == self.items:
            return QtCore.QModelIndex()
        return self.createIndex(parentNode.row(), 0, parentNode)

    def index(self, row, column, parentIndex):
        parentNode = self.getNodeFromIndex(parentIndex)
        childItem = parentNode.child(row)
        if childItem:            
            newIndex=self.createIndex(row, column, childItem)
            return newIndex
        else:
            return QtCore.QModelIndex()

    def rowCount(self, parent=QtCore.QModelIndex()):
        if not parent.isValid():
            parentNode = self.items
        else:
            parentNode = parent.internalPointer()
        return parentNode.childCount()

    def data(self, index, role):
        if not index.isValid(): return QtCore.QVariant()
        row=index.row()
        column=index.column()
        node=index.internalPointer()

        if role==QtCore.Qt.DisplayRole:
            if column==0 and not self.columnCount():
                return QtCore.QModelIndex()
            else:
                return QtCore.QModelIndex()

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        mainLayout=QtGui.QHBoxLayout()
        self.setLayout(mainLayout)   

        self.dataModel=DataModel()

        self.viewA=QtGui.QTableView()
        self.viewA.setModel(self.dataModel)
        self.viewA.clicked.connect(self.onClick)

        mainLayout.addWidget(self.viewA)  
        self.show()

    def onClick(self, index):
        node=index.internalPointer()
        print node.name(), node.getParent().name()

window=Window()
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

通过相应的方式设计类并不清楚你的意思,但内部指针只是你在createIndex()的{​​{1}}的帮助下创建的。

请参阅createIndex的文档。

所以由你来存储有用的东西。