[PyQt] How to use QHash in PyQt?

donglongchao donglongchao at 163.com
Thu Feb 4 02:41:35 GMT 2010



在2010-02-03 20:59:35,"Phil Thompson" <phil at riverbankcomputing.com> 写道: >On Wed, 3 Feb 2010 19:24:28 +0800 (CST), donglongchao ><donglongchao at 163.com> >wrote: >> 在2010-02-03 17:41:17,"Phil Thompson" <phil at riverbankcomputing.com> >> 写道: >On Wed, 3 Feb 2010 10:31:44 +0800 (CST), donglongchao >>><donglongchao at 163.com> >wrote: >> Hi,all >>  >> I am newbie to PyQt and I >> have a question when I work on my app with >PyQt. >> I want to use a >> QDataStream to form a QHash object into a QByteArray and >> write it into >a >> QTcpSocket.But there is an error when I run my app said  >> NameError: >> global name 'QHash' is not defined.I have import everything >from >> >QtCore >> and QtGui.Also I can locate qhash.sip in my computer >> >> (/usr/share/sip/PyQt4/QtCore/qhash.sip).So I want to know wherer is the >>> >> problem and how to use QHash here. > >In C++ QHash is a template. For the >> classes that Qt provides a qHash() >implementation, PyQt implements the >> __hash__ method so that hash() returns >the value that qHash() returns. >>  >> But I still do not know how to create an object like QHash<int,QString>.I >> checked the file qhash.sip and find that  >>  QHash<TYPE1, TYPE2> is implemented as a Python dictionary.So is it >>  possible to convert a dict object into a QHash<TYPE1,TYPE2> object? > >No, because templates are instantiated at compile-time not run-time. > >> Because >>  I can not use a dict object with QDataStream's operator "<<" directly. > >You can read and write the dict items individually. > >Alternatively (if the dict can be pickled) you can use >QDataStream.writeQVariant(). The dict will be converted to a QVariant >automatically. Use QDataStream.readQVariant() to read it back and >QVariant.toPyObject() to get the dict from the QVariant. 

Oh,bad luck....I am using PyQt with version 4.5.3 while the method writeQVariant() for QDataStream has been available only since 4.7.So is updating to the latest version the only way to handle this problem?Or is there any other approach (some hack or trick one) to make it work?


Longchao
> >Phil 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100204/ade85d9d/attachment.html>


More information about the PyQt mailing list