<div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><div id="isForwardContent"><br><div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><div id="isForwardContent"><div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div class="gmail_quote"><div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><div id="m_-6393571990545416172m_-2704110467409774206m_-3875064590240285110isForwardContent"><div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><pre style="width:1237.84375px"><div><div class="h5 __reader_view_article_wrap_7248539761125425__"><span style="line-height:23.3240013122559px">Hello everyone,

</span><div style="line-height:23.3240013122559px">I encountered an ¡°TypeError¡± that my python subclass listmodel of QAbsctactListModel object can not be recognized as a  C++ 'QAbstractListModel*' instance£¡</div><div style="line-height:23.3240013122559px">In order to simplify the problem, I simplified the project code to a demo attachments and also listed below the contents of the email.</div><div><span style="line-height:23.3240013122559px">The demo has two python subclass of QAbsctactListModel £ºRuleModel and RuleGroupModel£»RuleModel instance will be a role of </span><span style="line-height: 23.324px;">RuleGroupModel</span><span style="line-height: 23.324px;"> </span></div><div><span style="line-height: 23.324px;"> to provide rules of a group.I test it with PyQt5.7.1 and PyQt5.8.2 ,both </span><span style="line-height: 23.324px;">encountered the same Type</span><span style="line-height: 23.324px;"> Error which crashed the app is£º</span></div><div><span style="line-height: 23.324px;">        " </span><span style="line-height: 23.324px;">TypeError: invalid result from RuleGroupModel.data(), unable to convert a Python 'RuleModel' object to a C++ 'QAbstractListModel*' instance ".</span></div><div><span style="line-height:23.3240013122559px">But  when the count of changing model of Qml GridView "many times" £¬the error raised.</span><span style="line-height: 23.324px;">I am confused thar the error do not occure within a few times such as 5 or more times£»</span></div><div><span style="line-height: 23.324px;">The error will surely occure after more times of rule </span><span style="line-height: 23.324px;">model change</span><span style="line-height: 23.324px;">.To </span><span style="font-family: Î¢ÈíÑźÚ, ËÎÌå; font-size: 12px; line-height: 18px; white-space: normal;">reproduce the error I use a Timer in the Test.qml to simulate many times of user actions.</span></div><div><span style="line-height:23.3240013122559px">Any advice to the e</span></div><div><span style="line-height:23.3240013122559px">Thanks in advance£¡</span></div><div><span style="line-height:23.3240013122559px"><br></span></div><div><span style="line-height:23.3240013122559px">The demo code£º</span></div><div><span style="line-height:23.3240013122559px">#### testmodel.py##################<wbr></wbr>#####</span></div></div></div><div><span style="line-height:23.3240013122559px"><div><div class="h5">#encoding:utf8
'''
file:testmodel.py
'''
# import cgitb
# cgitb.enable()
import sys

from PyQt5.QtCore import QUrl, QAbstractListModel,QModelIndex<wbr></wbr>,pyqtSlot
from PyQt5 import QtQml
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView

class RuleModel(QAbstractListModel):
    def __init__(self,name=2,parent=No<wbr></wbr>ne):
        super().__init__(parent)
        self._datas = [{"name":"name %s" % k} for k in range(name)]
    def setDS(self,ds):
        self._datas = ds
    def roleNames(self):
        return {300:"name".encode()}
    def data(self,index,role=500):
        return self._datas[index.row()][self.<wbr></wbr>roleNames().get(role).decode()<wbr></wbr>]
    def rowCount(self,parent=QModelInd<wbr></wbr>ex()):
        return len(self._datas)

class RuleGroupModel(QAbstractListMo<wbr></wbr>del):
    def __init__(self,parent=None):
        super().__init__(parent)
        self._datas = [{"name":"na1" ,"rulesModel":RuleModel(name=1<wbr></wbr>)},
            {"name":"na2" ,"rulesModel":RuleModel(name=2<wbr></wbr>)},
            {"name":"na3" ,"rulesModel":RuleModel(name=3<wbr></wbr>)},
            {"name":"na4" ,"rulesModel":RuleModel(name=4<wbr></wbr>)}
        ]
    def setDS(self,ds):
        self._datas = ds
    @pyqtSlot(str,result=int)
    def roleValue(self, roleName):
        if roleName == 'name':
            return 300
        elif roleName == 'rulesModel':
            return 500
        return -1
    def roleNames(self):
        return {300:"name".encode(),500:"rule<wbr></wbr>sModel".encode()}
    def data(self,index,role=500):
        return self._datas[index.row()][self.<wbr></wbr>roleNames().get(role).decode()<wbr></wbr>]
    def rowCount(self,parent=QModelInd<wbr></wbr>ex()):
        return len(self._datas)
    
    
if __name__ == '__main__':

    
    app = QGuiApplication(sys.argv)

    view = QQuickView()
    
    engine = view.engine()
    engine.quit.connect(app.quit)
    
    modelA = RuleGroupModel()
    engine.rootContext().setContex<wbr></wbr>tProperty('ruleGroupsModel', modelA)


    view.setResizeMode(QQuickView.<wbr></wbr>SizeRootObjectToView)
    QtQml.qmlRegisterType(RuleGrou<wbr></wbr>pModel, 'RuleGroupModel', 1, 0, 'RuleGroupModel')
    QtQml.qmlRegisterType(RuleMode<wbr></wbr></div></div>l, 'RuleModel', 1, 0, 'RuleModel')

    view.setSource(QUrl('Test.qml'<wbr></wbr>))
    view.show()

    sys.exit(app.exec_())</span></div><span class=""><div><span style="line-height:23.3240013122559px">#### end of testmodel.py##################<wbr></wbr>#####</span></div><div><span style="line-height:23.3240013122559px">#### Test.qml #######################</span></div><div><span style="line-height:23.3240013122559px"><br></span><span style="line-height:1.7;color:rgb(128,128,0)">import</span><span style="line-height:1.7;color:rgb(192,192,192)"> </span><span style="line-height:1.7">QtQuick</span><span style="line-height:1.7;color:rgb(192,192,192)"> </span><span style="line-height:1.7">2.7</span></div>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#808000">import</span><span style="color:#c0c0c0"> </span>QtQuick.Controls<span style="color:#c0c0c0"> </span>2.0</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#808000">import</span><span style="color:#c0c0c0"> </span>QtQuick.Layouts<span style="color:#c0c0c0"> </span>1.0</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
</span><div><div class="h5"><pre style="margin-top:0px;margin-bottom:0px"><span style="color:#800080">Page</span><span style="color:#c0c0c0"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span><span style="color:#800000">width</span>:<span style="color:#c0c0c0"> </span>300</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span><span style="color:#800000">height</span>:<span style="color:#c0c0c0"> </span>600</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span><span style="color:#800000">header</span>:<span style="color:#c0c0c0"> </span><span style="color:#800080">ComboBox</span><span style="color:#c0c0c0"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">id</span>:<span style="color:#c0c0c0"> </span><span style="font-style:italic">groups</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">currentIndex</span>:<span style="color:#c0c0c0"> </span>-1</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">displayText</span>:<span style="color:#c0c0c0"> </span><span style="font-style:italic">currentIndex</span><span style="color:#c0c0c0"> </span>==<span style="color:#c0c0c0"> </span>-1<span style="color:#c0c0c0"> </span>?<span style="color:#c0c0c0"> </span><span style="color:#008000">"select</span><span style="color:#c0c0c0"> </span><span style="color:#008000">a</span><span style="color:#c0c0c0"> </span><span style="color:#008000">group"</span><span style="color:#c0c0c0"> </span>:<span style="color:#c0c0c0"> </span><span style="font-style:italic">currentText</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">model</span>:<span style="color:#c0c0c0"> </span>ruleGroupsModel</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">textRole</span>:<span style="color:#c0c0c0"> </span><span style="color:#008000">"name"</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">onCurrentIndexChanged</span>:<span style="color:#c0c0c0"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span><span style="font-style:italic">rulesView</span>.model<span style="color:#c0c0c0"> </span>=<span style="color:#c0c0c0"> </span><span style="font-style:italic">model</span>.data(<span style="font-style:italic">model</span>.index(<span style="font-style:italic">current<wbr></wbr>Index</span>,<span style="color:#c0c0c0"> </span>0),</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">                                         </span><span style="font-style:italic">model</span>.roleValue(<span style="color:#008000">'rulesModel'</span>))</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span><span style="color:#800080">Timer</span><span style="color:#c0c0c0"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">id</span>:<span style="color:#c0c0c0"> </span><span style="font-style:italic">timer</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">interval</span>:<span style="color:#c0c0c0"> </span>1000</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">repeat</span>:<span style="color:#c0c0c0"> </span>true</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">running</span>:<span style="color:#c0c0c0"> </span>false</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#808000">property</span><span style="color:#c0c0c0"> </span><span style="color:#808000">int</span><span style="color:#c0c0c0"> </span><span style="color:#800000">count</span>:<span style="color:#c0c0c0"> </span>0</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">onTriggered</span>:<span style="color:#c0c0c0"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span><span style="font-style:italic">count</span><span style="color:#c0c0c0"> </span>+=<span style="color:#c0c0c0"> </span>1</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span><span style="color:#808000">var</span><span style="color:#c0c0c0"> </span><span style="font-style:italic;color:#2985c7">idx</span><span style="color:#c0c0c0"> </span>=<span style="color:#c0c0c0"> </span><span style="font-style:italic">count</span><span style="color:#c0c0c0"> </span>%<span style="color:#c0c0c0"> </span><span style="font-style:italic">groups</span>.model.rowCount()</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span><span style="font-style:italic;color:#0055af">console</span>.log(<span style="color:#008000">'timer</span><span style="color:#c0c0c0"> </span><span style="color:#008000">trigger'</span>,<span style="color:#c0c0c0"> </span><span style="font-style:italic;color:#2985c7">idx</span>,<span style="color:#c0c0c0"> </span><span style="font-style:italic">count</span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span><span style="font-style:italic">groups</span>.currentIndex<span style="color:#c0c0c0"> </span>=<span style="color:#c0c0c0"> </span><span style="font-style:italic;color:#2985c7">idx</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span><span style="color:#800080">Component</span><span style="color:#c0c0c0"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">id</span>:<span style="color:#c0c0c0"> </span><span style="font-style:italic">ruleDelegate</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800080">RowLayout</span><span style="color:#c0c0c0"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span><span style="color:#800000">id</span>:<span style="color:#c0c0c0"> </span><span style="font-style:italic">ruleName</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span><span style="color:#800000">width</span>:<span style="color:#c0c0c0"> </span>300</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span><span style="color:#800000">height</span>:<span style="color:#c0c0c0"> </span>40</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span><span style="color:#800080">Label</span><span style="color:#c0c0c0"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">                </span><span style="color:#800000">verticalAlignment</span>:<span style="color:#c0c0c0"> </span><span style="color:#800080">Text</span>.AlignVCenter</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">                </span><span style="color:#800000">horizontalAlignment</span>:<span style="color:#c0c0c0"> </span><span style="color:#800080">Text</span>.AlignHCenter</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">                </span><span style="color:#800000">text</span>:<span style="color:#c0c0c0"> </span>[<span style="color:#008000">"<b>"</span>,<span style="color:#c0c0c0"> </span><a href="http://model.name" target="_blank">model.name</a>,<span style="color:#c0c0c0"> </span><span style="color:#008000">"<b>"</span>].join(<span style="color:#008000">''</span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">                </span><span style="color:#800000">Layout.fillWidth</span>:<span style="color:#c0c0c0"> </span>true</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">                </span><span style="color:#800000">Layout.preferredWidth</span>:<span style="color:#c0c0c0"> </span>3</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">            </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="line-height:1.7;color:rgb(192,192,192)">    </span><span style="line-height:1.7;color:rgb(128,0,128)">GridView</span><span style="line-height:1.7;color:rgb(192,192,192)"> </span><span style="line-height:1.7">{</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">id</span>:<span style="color:#c0c0c0"> </span><span style="font-style:italic">rulesView</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">anchors.fill</span>:<span style="color:#c0c0c0"> </span><span style="font-style:italic">parent</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">cellHeight</span>:<span style="color:#c0c0c0"> </span>50</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">cellWidth</span>:<span style="color:#c0c0c0"> </span>220</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="color:#800000">delegate</span>:<span style="color:#c0c0c0"> </span><span style="font-style:italic">ruleDelegate</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="line-height:1.7;color:rgb(192,192,192)">    </span><span style="line-height:1.7;color:rgb(128,0,0)">Component.onCompleted</span><span style="line-height:1.7">:</span><span style="line-height:1.7;color:rgb(192,192,192)"> </span><span style="line-height:1.7">{</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="font-style:italic">timer</span>.running<span style="color:#c0c0c0"> </span>=<span style="color:#c0c0c0"> </span>true</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">        </span><span style="font-style:italic;color:#0055af">console</span>.log(<span style="color:#008000">'mainload'</span>,<span style="color:#c0c0c0"> </span>ruleGroupsModel)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:#c0c0c0">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px">}</pre>####### end of Test.qml ############
<pre style="margin-top:0px;margin-bottom:0px"><br></pre><span style="line-height:23.3240013122559px">Thanks !</span></div></div></pre></div></div></div></div></div></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">songchunyi</div>
</div>
</div></div><br><br><span title="neteasefooter"><p> </p></span></div></div><br><br><span title="neteasefooter"><p> </p></span>