2010-11-07 41 views
6

Tôi đang sử dụng PyQt để quản lý chế độ xem dạng cây bằng cách sử dụng QAbstractItemModel. Cho đến nay tôi đã triển khai thành công nó sao cho tôi có thể tải dữ liệu, mở rộng và thu gọn dữ liệu và chỉnh sửa các giá trị.pyqt: Cố gắng hiểu các chèn cho QAbstractDataModel và QTreeView

Một điều tôi không thể làm, tuy nhiên, là quấn đầu của tôi xung quanh chèn và loại bỏ hàng.

phiên bản ngắn về những gì tôi đang cố gắng để làm:

Khi người dùng chỉnh sửa một tế bào đặc biệt, tôi cần phải thực sự xóa các mục cơ bản trong hệ thống phân cấp đối tượng của tôi và thay thế nó bằng một hình khác nhau. Tôi thực hiện điều này trong phương thức setData của mô hình của tôi. Vì tôi không hoàn toàn hiểu những gì tôi đang làm tôi dường như đã thiết lập nó để nó segfaults. Về cơ bản, tôi chỉ cần hiểu rõ hơn về cách mô hình dữ liệu tương tác với QModelIndex, nhưng đọc và đọc lại các tài liệu dường như không khai sáng cho tôi. Bất kỳ trợ giúp (hoặc bất kỳ liên kết đến một hướng dẫn phong nha - tốt nhất, mặc dù không nhất thiết, trong python - cũng sẽ được đánh giá rất nhiều).

Đây là một mẫu của mã Tôi đang sử dụng:

#--------------------------------------------------------------------------- 
def setData(self, index, value, role=QtCore.Qt.EditRole): 
    """ 
    Sets the data. 
    """ 
    if index.isValid() and (0 <= index.row() < self.rowCount()): 

     item = index.internalPointer() 
     value = value.toString() 
     if index.column() == 5: 
      # rip out the current object and replace it with a new one of 
      # the correct datatype. 

      #next 4 lines get info from my underlying hierarchy of objects 
      dataType = str(value) 
      parent = item.get_parent() 
      name = item.get_name() 
      row = parent.get_row_of_child(name) 

      #assuming everything is ok, I now am trying to manage the 
      #underlying objects 
      if row != None: 

       #I am calling this because I think I need to, but don't 
       #really know if it is called for here or not 
       self.beginInsertRows(self.parent(index), row, 1) 

       #Next 3 lines create and initialize a new underlying 
       #object that will be inserted. 
       newItem = self.root.create_template_param_obj(dataType, 
                   name, 
                   parent) 
       newItem.set_index(row) 
       newItem.set_default_value(item.get_default_value()) 

       #now I remove the old object from my underlying 
       #hierarchy and insert the new one 
       parent.remove_child_at_row(row) 
       parent.insert_child_at_row(newItem, row) 

       #this is where I get lost. I *think* I need to point to 
       #the new underlying object (i.e. rebuild the index) 
       #so I am going to call the data model's index method. 
       #But that needs the index of the parent, so first I 
       #call the data model's parent method to get the index 
       #of the parent. But this code segfaults (I think it 
       #is the treeview that actually freaks out because this 
       #setData method completes properly before the whole thing 
       #crashes. Does anyone have a pointer to a decent tutorial 
       #that will explain how data models and mode indexes work? 
       self.index(row, 5, self.parent(index)) 
       self.endInsertRows() 

     self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), 
           index, index) 
     return True 

    #Ignore any role other than the edit role 
    return False 

#--------------------------------------------------------------------------- 
def index(self, row, column, parent): 
    """ 
    Connect the data model to the actual object hierarchy. 
    """ 
    if not self.hasIndex(row, column, parent): 
     return QtCore.QModelIndex() 

    if not parent.isValid(): 
     parentItem = self.root 
    else: 
     parentItem = parent.internalPointer() 

    childItem = parentItem.get_child_at_row(row) 
    if childItem: 
     return self.createIndex(row, column, childItem) 
    else: 
     return QtCore.QModelIndex() 


#--------------------------------------------------------------------------- 
def parent(self, index): 
    """ 
    Returns a QModelIndex of the parent 
    """ 
    if not index.isValid(): 
     return QtCore.QModelIndex() 

    childItem = index.internalPointer() 
    if not childItem: 
     return QtCore.QModelIndex() 

    parentItem = childItem.get_parent() 

    if parentItem == self.root: 
     return QtCore.QModelIndex() 

    return self.createIndex(parentItem.get_index(), 0, parentItem) 

Trả lời

2

tôi sẽ cố gắng và cung cấp cho bạn một số lời khuyên, tôi nhớ rằng phần này đã làm người đứng đầu của tôi trong cũng như khi tôi phải thực hiện nó cho ứng dụng của tôi !

Vì vậy, từ những gì tôi nhớ, bạn phải thực hiện các phương pháp ảo sau:

virtual bool insertRows(int Row, int Count, const QModelIndex& rParent); 
virtual bool removeRows(int Row, int Count, const QModelIndex& rParent = QModelIndex()); 

bool SuperModel::insertRows(int Row, int Count, const QModelIndex& rParent) 
{ 
... 
    // On débute l'insertion des lignes. 
    beginInsertRows(rParent, Row, Row + Count -1); 
    // ... Perform insertion here, you'll have something like 
    pParent->addChild(Row); 
    endInsertRows(); 
} 

bool SuperModel::removeRows(int Row, int Count, const QModelIndex& rParent) 
{ 
    ... 
    beginRemoveRows(rParent, Row, Row + Count -1); 
    // ... Perform removing here, you'll have something like 
    pParent->removeChild(Row); 
    endRemoveRows(); 
} 

Một số thông tin thêm: http://doc.qt.io/archives/qt-4.7/qabstractitemmodel.html#insertRows

Tôi hy vọng nó sẽ giúp bạn một chút ... nó không có trong PyQt nhưng hy vọng nó sẽ cung cấp cho bạn một số lời khuyên ...

+0

Andy, đó là nó. Cảm ơn. Với sự giúp đỡ của bạn, tôi thậm chí có thể làm cho nó hoạt động mà không có hai phương pháp. Tôi chỉ cần gọi: self.beginRemoveRows (self.parent (index), row, row) trước khi xóa hàng và self.endRemoveRows() sau. Tương tự với việc chèn đối tượng mới. Dường như rõ ràng bây giờ nhưng tôi không thể tìm ra nó nếu không có sự giúp đỡ của bạn. Cảm ơn một lần nữa! – bvz

+0

Wicked! Công việc tốt! –

Các vấn đề liên quan