PyQt QWidget in QAbstractListModel gets deleted with QSortFilterProxyModel -
i need populate listview widgets, have custom proxyfilter work it. without filter works great, when active seems delete widgets attach model.
it shows fine showing items, filtering works when erasing filter, when hidden widgets should shown again following error gets thrown:
custom_widget.setgeometry(option.rect) runtimeerror: underlying c/c++ object has been deleted
tried not using qvariant , going internalpointer route breaks @ same spot.
thanks having look!
setup:
def __init__(self, *args): qtgui.qwidget.__init__(self, *args) # create temp data self.list_data = [] x in xrange(500): widget = listitemwidget(text=str(x), parent=self) self.list_data.append((str(x), widget)) # testing put in inmut tuple # create listviewmodel self.lm = listviewmodel(parent=self) # create listview widget self.lv = qtgui.qlistview() # create filter proxy self.proxy_model = listviewfilterproxymodel() self.proxy_model.setfilterpattern('') self.proxy_model.setsourcemodel(self.lm) # set model of listview filter proxy self.lv.setmodel(self.proxy_model) # set delegate column 0 self.lv.setitemdelegateforcolumn(0, customwidgetdelegate(self.lv)) self.lm.updatedata(self.list_data) self.proxy_model.invalidate() self.connect(self.filter_edit, qtcore.signal("textchanged(qstring)"), self.update_filter) def update_filter(self, pattern): self.proxy_model.setfilterpattern(pattern) self.proxy_model.invalidate()
custom widget
class listitemwidget(qtgui.qwidget): def __init__(self, text=none, parent=none): qtgui.qwidget.__init__(self) self.text = text @qtcore.pyqtproperty(qtcore.qstring) def text(self): return self.__text @text.setter def text(self, value): self.__text = value
delegate painting view
class customwidgetdelegate(qtgui.qitemdelegate): def __init__(self, parent=none): super(customwidgetdelegate, self).__init__(parent) def paint(self, painter, option, index): custom_widget = index.model().data(index, qtcore.qt.displayrole).topyobject()[1] >>>>>> custom_widget.setgeometry(option.rect) if not self.parent().indexwidget(index): self.parent().setindexwidget(index, custom_widget)
list view model:
class listviewmodel(qtcore.qabstractlistmodel): def __init__(self, parent=none, *args): qtcore.qabstractlistmodel.__init__(self, parent, *args) self.listdata = [] def rowcount(self, parent=qtcore.qmodelindex()): return len(self.listdata) def data(self, index, role): if role == qtcore.qt.sizehintrole: return qtcore.qsize(80, 80) if index.isvalid() , role == qtcore.qt.displayrole: return qtcore.qvariant(self.listdata[index.row()]).topyobject() return qtcore.qvariant() def updatedata(self, listdata): self.listdata = listdata index = len(self.listdata) return true
finally filter proxy model:
class listviewfilterproxymodel(qtgui.qsortfilterproxymodel): def __init__(self, parent=none): self.filter_str = none qtgui.qsortfilterproxymodel.__init__(self, parent) def setfilterpattern(self, pattern): self.filter_str = qtcore.qstring(pattern) def filteracceptsrow(self, sourcerow, sourceparent): if self.filter_str none: return true index = self.sourcemodel().index(sourcerow, 0, sourceparent) # testing on str here... text = index.data().topyobject()[0] if not str(self.filter_str) in text: return false return true
Comments
Post a Comment