qml listview with c++ nest listmodel crash when pop from stackview



  • i have a c++ nest listmodel structure like this

    class ListModel : public AbstractListModel
    {
    public:
    .....
    private:
    QList<QObject*> m_List;
    }

    class TrendListModel : public ListModel
    {
    public:
    .....
    void addTrendData(.....)
    {
    TrendData* trendData = new TrendData(....);

        QQmlEngine::setObjectOwnership(trendData, QQmlEngine::CppOwnership);
    
        beginInsertRows(QModelIndex(), rowCount(), rowCount());
        m_List << trendData;
        endInsertRows();
    }
    
    void addImage(int row, QString path)
    {
        if(row < 0 || row >= rowCount())
        {
            qWarning() << "addImage to invalid trendData pointer!"
            return;
        }
        TrendData* trendData = qobject_cast<TrendData*>(m_List.at(row));
        trendData->addImage(...., path);
    
        emit dataChanged(index(row), index(row));
    }
    

    }

    class TrendData : public QObject
    {
    public:
    .....
    {
    m_ImageListModel = new ImageListModel(this);
    QQmlEngine::setObjectOwnership(m_ImageListModel, QQmlEngine::CppOwnership);
    }
    void addImage(..., path)
    {
    m_ImageListModel->addImage(..., path);
    }
    private:
    ImageListModel* m_ImageListModel;
    }

    class ImageListModel : public ListModel
    {
    void addImage(..., path)
    {
    Image* image = new Image(path, this);

         QQmlEngine::setObjectOwnership(image, QQmlEngine::CppOwnership);
         beginInsertRows(QModelIndex(), rowCount(), rowCount());
         m_List << image;
         endInsertRows();
     }
    

    }

    ListView{
    model: trendListModel //from setContextProperty()

    delegate{
        .......
        ListView{
            model: imageListModel //return from trendListModel's data function
        }
    }
    

    }

    i append the instance of TrendListModel to qml listview, preview correctly, but when i pop the qml page from stackview, it crash on ios, but works fine on android. Attact crash report as below:

    0_1456285215767_crash.png



  • @liweian

    Order code follow:

    cpp:

    //~ ListModel
    AbstractListModel {
        QList<QObject*> m_List;
    }
    
    //~ TrendData
    QObject{
    
        ImageListModel* m_ImageListModel;
        function addImage(..., path){
            m_ImageListModel->addImage(..., path);
        }
    }
    
    //~ TrendListModel
    ListModel {
        function addTrendData(){
            var trendData = new TrendData();
            begineInsertRows();
            this->m_list << trendData;
            endInsertRows();
        }
        function addImage(row, path){
            if(something is not ok) return;
            
            var trendData = m_list.at(row);
            // here check trendData vaild.
            // if qobject_cast fail.
            //! [0]
            if(trendData == null) return;
            //! [0]
            trendData->addImage(..., path);
            emit dataChanged(index(row), index(row));
        }
    }
    
    class Image;
    
    //~ ImageListModel
    ListModel {
        function addImage(..., path){
            var image = new Image;
            beginInserRows();
            this->m_list << image;
            endInsertRows();
        }
    }
    
    

    qml:

    ListView {
        model: trendListModel // from setContextProperty()
        delegate: ListView {
            model: imageListModel //return from trendListModel's data function
        }
    }
    

Log in to reply
 

走马观花

最近的回复

  • E

    是不是好久都没有人了,站主找到工作了,还是说划水了

    read more
  • 请问一下如果原Qt桌面软件支持拖拽外部文件进去(比如把在用资源管理器里的一张图片拖拽到软件界面上就会显示这张图片),转成Webassembly之后这种拖拽操作还是否有效?劳烦大佬帮我试试看,可以的话我就学QML了

    read more
  • H
    Toou 2D 拿来即用,为简单而生。

    简称T2D,是一款采用自身模块规范编写的轻量级UI框架,遵循Qt书写与组织形式门槛极低无需深入学习简单易用可拿来即用,丰富的控件模块适合界面的快速开发,让程序人员拥有更多的精力来实现业务逻辑与算法。

    统一交互规范,丰富的Ui控件几十种常用控件放弃了Qt Controls 及 Controls 2 来提高性能。

    完善的主题系统,业务逻辑与界面主题设计分离,可通过简单修改变量自定义主题皮肤。灵活的多主题皮肤绑定机制、在不需要重启App即实现一键换肤

    ini皮肤配置规则与每一个控件融合。可在应用内配置也可在应用外动态扩展配置。

    框架自动化安装支持动态库、静态库多模式编译。使用方便更安全更自由。

    提供丰富Demo、全面的帮助文档,Api查阅快速方便。项目必备开源框架!

    已经集成最新版 Font Awesome 4.7

    开源地址:https://github.com/ShowFL/Toou-2D

    read more
  • 刚刚毕业,工作用QT开发,以后有问题多多请教各位前辈😬 抱拳了。

    read more

关注我们

微博
QQ群