PyQt beginremoverows

In the following example:

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 delete a row and its children (if any) when I click on it.
(The folder under the click and its children should be deleted.)

I know I'm wrong on this line:

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

Thank you for your time.

+3
source share
3 answers

I know that I am mistaken in this line:

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

Yes you are right. Let's see what you go through:

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 image in the beginRemoveRows documentation :

Having said that, you want to remove from index.row()to a string equal to the number of children by index. Your incorrect matches with your parent and child indexes.

What did you really want:

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

index.row(), .

, : beginRemoveRows() . , . endRemoveRows(), , , .

++ beginRemoveRows(), , .

, , - (.. QSortFilterProxyModel), , . QSortFilterProxy .

+3

Jebagnanadas - ; , TreeView, / TreeView.

test() -, refresh() ( ), TreeView -.

, , , QT-, .

0

jcoon kaleb.. , setRowHidden() .

0

Source: https://habr.com/ru/post/1732262/


All Articles