pyqt:试图了解QAbstractDataModel和QTreeView的insertrows [英] pyqt: Trying to understand insertrows for QAbstractDataModel and QTreeView

查看:113
本文介绍了pyqt:试图了解QAbstractDataModel和QTreeView的insertrows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyQt使用QAbstractItemModel管理树视图.到目前为止,我已经成功实现了它,以便可以加载数据,展开和折叠数据以及编辑值.

但是,我无法做的一件事就是将头缠在插入和删除行上.

我正在尝试做的简短版本:

当用户编辑特定的单元格时,我实际上需要删除对象层次结构中的基础项目,并将其替换为其他项目.我在模型的setData方法中实现了这一点.由于我不完全了解自己在做什么,因此我似乎已经对其进行了设置,以使其发生段错误.

基本上,我只需要更好地了解数据模型如何与QModelIndex交互,但是阅读和重新阅读文档似乎并没有启发我.任何帮助(或任何指向体面的教程的链接-最好但不一定是python)也将不胜感激.

以下是我正在使用的代码示例:

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

解决方案

我将尝试给您一些提示,我记得当我必须为我的应用程序实现它时,这一部分也引起了我的注意! >

因此,据我所记得,您必须实现以下虚拟方法:

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

更多信息: http://doc.qt.io/archives/qt-4.7/qabstractitemmodel.html#insertRows

我希望它能对您有所帮助...它不在PyQt中,但希望它会给您一些提示...

I am using PyQt to manage a tree view using a QAbstractItemModel. So far I have successfully implemented it such that I can load the data, expand and collapse it, and edit values.

One thing I am not able to do, however, is wrap my head around inserting and removing rows.

Short version of what I am trying to do:

When the user edits a particular cell, I need to actually delete the underlying item in my object hierarchy and replace it with a different one. I implement this in the setData method of my model. Since I don't completely understand what I am doing I seem to have set it up so that it segfaults.

Basically, I just need to get a better understanding of how the data model interacts with QModelIndex, but reading and re-reading the docs does not seem to enlighten me. Any help (or any links to a decent tutorial - preferably, though not necessarily, in python - would also be greatly appreciated).

Here is a sample of the code I am using:

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

解决方案

I'll try and give you some tips, I remember that this part did my head in as well when I had to implement it for my application !

So, from what I remember, you have to implement the following virtual methods :

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

Some more informations : http://doc.qt.io/archives/qt-4.7/qabstractitemmodel.html#insertRows

I hope it will help you a bit... it's not in PyQt but hopefully it will give you some tips...

这篇关于pyqt:试图了解QAbstractDataModel和QTreeView的insertrows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆