PyQt 开始删除行

发布于 2024-08-21 05:13:16 字数 1835 浏览 3 评论 0原文

在下面的示例中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我早已燃尽 2024-08-28 05:13:16

我知道我在这件事上犯了错误
行:

self.model.beginRemoveRows(index.parent(),index.row(),self.model.rowCount(index))

是的,你说得对。让我们看看您传递的内容:

index.parent() - the parent of index
index.row() - the row number of index, the row you want deleted
self.model.rowCount(index) - the number of total children had by index

现在,看看 beginRemoveRows

告诉它您要从 index.row() 中删除等于索引子级数量的行。你的亲子指数不匹配。

您真正想要的是:

beginRemoveRows(index.parent(), index.row(), index.row())

如果您删除 index.row() 处的行,它的所有子项都将自动删除

但是,还有一个更大的问题:beginRemoveRows() 不会删除任何行。它只是提醒您的模型您将要删除行。当您调用 endRemoveRows() 时,模型会让任何监听者知道它已更新,以便他们可以正确重绘。

在 C++ 中,您不允许调用 beginRemoveRows(),因为它们是受保护的方法,只有模型才能调用。

要根据需要进行过滤,您需要创建一个自定义代理模型(即 QSortFilterProxyModel< /a>) 执行您想要的过滤。然后,您将在信号处理程序中操作 QSortFilterProxy 模型作为响应。

I know I'm making mistake on this
line:

self.model.beginRemoveRows(index.parent(),index.row(),self.model.rowCount(index))

Yes, you're right. Let's look at what you're passing in:

index.parent() - the parent of index
index.row() - the row number of index, the row you want deleted
self.model.rowCount(index) - the number of total children had by index

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:

beginRemoveRows(index.parent(), index.row(), index.row())

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 call endRemoveRows(), 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.

感情废物 2024-08-28 05:13:16

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 a refresh() 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.

小兔几 2024-08-28 05:13:16

谢谢 jcoon 和 kaleb ..我已经使用 setRowHidden() 函数从树视图中隐藏了该行..

Thanks jcoon and kaleb.. I've hidden the row using setRowHidden() function from the tree view..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文