- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 PyQt 来管理使用 QAbstractItemModel 的树 View 。到目前为止,我已经成功地实现了它,以便我可以加载数据、展开和折叠它以及编辑值。
但是,我无法做的一件事是将我的头围绕在插入和删除行上。
我正在尝试做的简短版本:
当用户编辑特定单元格时,我需要实际删除对象层次结构中的底层项目并将其替换为不同的项目。我在我的模型的 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();
}
关于qt - pyqt:试图了解 QAbstractDataModel 和 QTreeView 的插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4119793/
有人知道如何为 QTreeView 项目的子组实现/实现具有不同颜色的 QTreeView 吗? 像这样的东西: 有没有人做过类似的事情,可以给我一个教程或操作方法的链接,或者示例代码也不错。目前我完
在 Qt 文档中 http://doc.qt.io/qt-5/model-view-programming.html#model-subclassing-reference ,据说,如果你的模型是可排
我正在使用 Qt 4.7.0,一个多列的 Qtreeview。 我想要做的是“简单”:我想要一条线增加它的高度 , 当它被选中时。 代表是否足以做到这一点? 我已经通过 QTableView 经历了一
我需要在 QTreeView 中通过拖放来实现行移动,并在行之间显示放置指示器。我想知道是否有一种方法可以覆盖指标绘图,因此它仅针对行之间的所有层次结构显示(不是项目周围的矩形),该行必须与整行一样宽
我现在正在测试 QTreeView 功能,我对一件事感到惊讶。似乎 QTreeView 内存消耗取决于项目数 O_O。这是非常不寻常的,因为这种类型的模型 View 容器只跟踪显示的项目,其余项目都在
我正在尝试让 QTreeView(使用底层 QFileSystemModel)显示目录树。如果我将 RootPath 设置为父目录,那么我会看到所有子目录,但看不到父目录。如果我将 RootPath
我尝试编写一个基于 QTreeView 的可编辑表格,在单元格中包含自动换行的内容,而这些单元格又应该像 MSWord 中表格中的普通单元格一样扩展高度(整个文本可见),但遇到了无法克服的障碍: 首先
我想要一个 TreeView ,它在各个列中显示项目名称、项目描述和两个相关的 bool 值。我首先修改 Editable Tree Mode example ,所以有一个 TreeModel 跟踪一
我想在我的程序中实现一个带有嵌套子级别的树,我正在寻找这两种( View /小部件)中的哪一种最适合我的目标。 我有一个任务完成/错过/失败的天数列表,每个任务都有完成/错过/失败的次数,最后是当天的
我想要我的 QTreeView总是展开所有的项目。在这种情况下,所有展开按钮/装饰都是不必要的,我想摆脱它们。我怎样才能删除所有这些? setRootIsDecorated只会删除第一级的按钮....
我有一个 QTreeView 子类(和 QAbstractItemModel 子类),它有一个漂亮的水平标题。我想添加垂直标题(从左侧向下)以匹配。但与具有单独的垂直 ( setVerticalHea
我想在 Qt 中实现对 QTreeView 对象的手动控制。 (这意味着将对一切进行编程控制,包括导航)到目前为止,我已经在 sibling 中实现了导航/选择。但是,我想建立一个更容易控制的状态,无
我想在 Qt 中实现对 QTreeView 对象的手动控制。 (这意味着将对一切进行编程控制,包括导航)到目前为止,我已经在 sibling 中实现了导航/选择。但是,我想建立一个更容易控制的状态,无
我有需要在 QTreeView 中显示的自定义数据。我从 QAbstractTableModel 派生了我的模型,并制作了自己的 rowCount()、columnCount()、data() 和 h
我的小部件中有一个QTreeView。当在 View 中选择一个项目时,我有 在详细信息窗口中更新一系列信息小部件的信号处理程序 关于所选项目。然后,用户可以编辑项目详细信息并提交 更改回模型。 更改
在下面的代码中,QTreeView 小部件始终占用最大空间,并为 QPushButton 小部件留下最小空间(代码后的图像)。如果 QTreeView 小部件被另一个 QPushButton 替换(当
我正在使用 Qt4,我正在尝试显示带有 QTreeView 的模型。我拥有的模型由多个批处理过程组成,每个批处理都有一些元素,每个元素都有一些子元素(同一批元素具有相同数量的子元素,但我认为这与问题无
我有一个 qt 对象 QTreeView我想切换标签位置。基本上我有一个填充的 TreeView。和功能 columnViewportPosition ( int column ) 第一列返回 0,第
我目前正在尝试制作一个 QTreeView 来显示计算机上文件夹的内容。但是,我遇到了一些奇怪的问题,其中 .和 .. 显示在 TreeView 中,我不希望这种情况发生。我该如何禁用显示 .以及 .
我在从 QTreeWidget 迁移到 QtreeView 时遇到问题。对于 QTreeWidget 来说显而易见且微不足道的事情在 View 中似乎是不可能的。具体来说:我有一个带有 TreeVie
我是一名优秀的程序员,十分优秀!