tags:

views:

218

answers:

3

Hi all,
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.

+1  A: 

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.

Kaleb Pederson
Thanks for replying kaleb. It's not working.. The folders (in the model) is not getting deleted as it has to be.. (It's getting deleted in some other order)..Can u please check this code in your system.. I don't know why this is happening???
Jebagnanadas
I just added a new section starting at "BUT" that hopefully explains the real problem (now assuming that the code you posted is complete and not just a sample).
Kaleb Pederson
Thank you for replying again and for clear explanation.. I'm totally new to model/view programming here.. Can u please post a code snippet of QSortFilterProxyModel that can be used here.. Because i haven't used this..
Jebagnanadas
A: 

Hi, 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.

jcoon
Thank you so much jcoon for your reply.. When i've tried as you said i'm getting unexpected behaviour when deleted!!!
Jebagnanadas
What does your new code look like so far?
jcoon
jcoon sorry for replying late.. I've hidden the selected directories using setRowHidden() in the tree view and as of now it looks sufficient enough.. Thanks for spending your time..
Jebagnanadas
A: 

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

Jebagnanadas