PDA

View Full Version : Deleting rows from qdirmodel..



pyqt123
10th February 2010, 21:50
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.r ow(),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 it's children (if it has) when i click on it.. (The folder under click and it's children have to be removed) I know i'm making mistake patricularly in this line..
self.model.beginRemoveRows(index.parent(),index.ro w(),self.model.rowCount(index))
Can u please help me in this regard .. Thanks..

ecanela
10th February 2010, 23:30
if you want remove items from a model, need call removeRows() function.

QAbstractItemModel::beginRemoveRowsand QAbstractItemModel::endRemoveRows () are used to notify when a item has removed from the model, but not actually remove any items of the model.

this functions are used when create a custom model and call removeRows() to remove item from the model.

pyqt123
11th February 2010, 09:19
this functions are used when create a custom model and call removeRows() to remove item from the model.

Thanks for your reply. But what should i do to remove rows from QDirModel...