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