[PyQt] using pickle to save contents of QSortFilterProxyModel

Phil Thompson phil at riverbankcomputing.com
Sat Nov 20 06:48:15 GMT 2010

On Fri, 19 Nov 2010 17:31:26 -0800 (PST), James Polk <jpolk5001 at yahoo.com>
> Hi,..I'm trying to save the contents of a QSortFilterProxyModel
> (self.proxyModel) as a pickle format file.
> The code below appears to work, but unfortunately, there's
> nothing in the file....zero size file..
>  def saveAsPickle(self):
>      fname = "C:/tmp/pick.dat"
>      file = open(unicode(fname), "w")
>      newModel = self.proxyModel
>      pickle.dump(newModel, file)
> So, I decided to take step back and save the file as simple text,
> and that works fine...so I know the data in self.proxyModel is
> good and is there...
> After several hours of tinkering and research,...I'm a bit stumped.
> I read somewhere...that the statement
>     newmodel = self.proxyModel
> was necessary,...or advised,..though I'm not totally clear why..

It makes no difference.

> Would it have something to do with the presence of an item in
> the database stored as
> QtCore.QDateTime(QtCore.QDate(2010,11,2)),
> ...which is also printed/saved out as that in the simple text save
> function...All other data in the model are simple strings...at least
> as of yet,...plan is to add an image/icon in there somewhere..
> Although there seem to be a lot of examples out there that 
> deal with basic use of pickle,..I haven't found any dealing with
> QSortFilterProxyModel....and/or   QTable-, QTree-, QListView...
> Is there another preferred way of saving data from these three
> elements?
> This is the class/init part at the top
> class Window(QtGui.QWidget):
>     def __init__(self):
>         super(Window, self).__init__()
>         self.proxyModel = QtGui.QSortFilterProxyModel()
>         etc
>         etc
> Any help or advice would be greatly appreciated,..

Pickling of QObject instances isn't supported because there is so much
state information (some of which you probably don't have access to) that
you would need to restore.


More information about the PyQt mailing list