PyQt 开始删除行
在下面的示例中:
from PyQt4 import QtCore, QtGui
class Ui_Dialog(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
self.setObjectName("Dialog")
self.resize(600, 500)
self.model = QtGui.QDirModel()
self.tree = QtGui.QTreeView()
self.tree.setModel(self.model)
print(self.model.flags(self.model.index("c:\Program Files")))
self.model.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)
self.tree.setSortingEnabled(True)
self.tree.setRootIndex(self.model.index("c:\Program Files"))
#self.tree.hideColumn(1)
#self.tree.hideColumn(2)
#self.tree.hideColumn(3)
self.tree.setWindowTitle("Dir View")
self.tree.resize(400, 480)
self.tree.setColumnWidth(0,200)
self.tree.show()
QtCore.QObject.connect(self.tree, QtCore.SIGNAL("clicked(QModelIndex)"), self.test)
QtCore.QMetaObject.connectSlotsByName(self)
self.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
def test(self,index):
print(self.model.filePath(index))
print(self.model.rowCount(index))
#self.model.beginRemoveRows(index.parent(),index.row(),self.model.rowCount(index))
#self.model.endRemoveRows()
print("Row of the index =",index.row())
print("Parent = ",self.model.data(index.parent()))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = Ui_Dialog()
#ui.show()
sys.exit(app.exec_())
我想在单击该行时删除该行及其子项(如果有)。
(单击下的文件夹及其子文件夹必须被删除。)
我知道我在这一行上犯了错误:
self.model.beginRemoveRows(index.parent(),index.row(),self.model.rowCount(index))
感谢您的宝贵时间。
In the example below:
from PyQt4 import QtCore, QtGui
class Ui_Dialog(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
self.setObjectName("Dialog")
self.resize(600, 500)
self.model = QtGui.QDirModel()
self.tree = QtGui.QTreeView()
self.tree.setModel(self.model)
print(self.model.flags(self.model.index("c:\Program Files")))
self.model.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)
self.tree.setSortingEnabled(True)
self.tree.setRootIndex(self.model.index("c:\Program Files"))
#self.tree.hideColumn(1)
#self.tree.hideColumn(2)
#self.tree.hideColumn(3)
self.tree.setWindowTitle("Dir View")
self.tree.resize(400, 480)
self.tree.setColumnWidth(0,200)
self.tree.show()
QtCore.QObject.connect(self.tree, QtCore.SIGNAL("clicked(QModelIndex)"), self.test)
QtCore.QMetaObject.connectSlotsByName(self)
self.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
def test(self,index):
print(self.model.filePath(index))
print(self.model.rowCount(index))
#self.model.beginRemoveRows(index.parent(),index.row(),self.model.rowCount(index))
#self.model.endRemoveRows()
print("Row of the index =",index.row())
print("Parent = ",self.model.data(index.parent()))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = Ui_Dialog()
#ui.show()
sys.exit(app.exec_())
I want to remove the row and its children (if any) when i click on it.
(The folder under click and it's children have to be removed.)
I know I'm making mistake on this line:
self.model.beginRemoveRows(index.parent(),index.row(),self.model.rowCount(index))
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,你说得对。让我们看看您传递的内容:
现在,看看 beginRemoveRows:
告诉它您要从
index.row()
中删除等于索引子级数量的行。你的亲子指数不匹配。您真正想要的是:
如果您删除
index.row()
处的行,它的所有子项都将自动删除。但是,还有一个更大的问题:
beginRemoveRows()
不会不删除任何行。它只是提醒您的模型您将要删除行。当您调用endRemoveRows()
时,模型会让任何监听者知道它已更新,以便他们可以正确重绘。在 C++ 中,您不允许调用
beginRemoveRows()
,因为它们是受保护的方法,只有模型才能调用。要根据需要进行过滤,您需要创建一个自定义代理模型(即 QSortFilterProxyModel< /a>) 执行您想要的过滤。然后,您将在信号处理程序中操作 QSortFilterProxy 模型作为响应。
Yes, you're right. Let's look at what you're passing in:
Now, take a look at the picture in the documentation on beginRemoveRows:
Your telling it that you want to remove from
index.row()
to the row equal to the number of children had by index. Your mis-matching your parent-child indices.What you really wanted was:
If you remove the row at
index.row()
, all of its children will be removed automatically.BUT, there's a bigger problem:
beginRemoveRows()
does NOT remove any rows. It simply alerts your model that you are going to be removing rows. When you callendRemoveRows()
, the model will then let anybody listening know that it's been updated so they can redraw correctly.In C++, you wouldn't be allowed to call
beginRemoveRows()
because they are protected methods that only the model is intended to call.To filter as you'd like, you'll need to create a custom proxy model (i.e. QSortFilterProxyModel) that does the filtering you want. You'll then manipulate the QSortFilterProxy model in response in your signal handler.
Jebagnanadas - 我建议稍微改变你的设计;不要使用 UI 作为模型和视图,而是创建单独的对象来表示 TreeView 中的内容,然后更新这些对象并重建/刷新 TreeView。
您的
test()
方法应该只从成员变量中删除选定的对象,然后调用refresh()
方法(您需要编写)来清除 TreeView并使用更新的成员变量重建它。这种设计更适合使用,因为它将 UI 与模型分开,并且您无需担心处理更多的 QT 方法。
Jebagnanadas - I recommend changing your design slightly; instead of using the UI as your model and view, create separate obects to represent what is in your TreeView, and then update those objects and rebuild/refresh your TreeView.
Your
test()
method should just remove the selected objects from a member variable, and then call arefresh()
method (that you need to write) that will clear the TreeView and rebuild it using the updated member variable.This design is much nicer to work with because it separates the UI from your model, and you don't need to worry about dealing with more QT methods then you have to.
谢谢 jcoon 和 kaleb ..我已经使用 setRowHidden() 函数从树视图中隐藏了该行..
Thanks jcoon and kaleb.. I've hidden the row using setRowHidden() function from the tree view..