Calling ListView's model crashes the application



  • Hi all,

    Would anyone have an idea why my application crashes when I'm calling the model of my ListView? I'm creating a list which has text and a checkbox on each row. On my qml file I'm creating the list like this:
    @
    ListView {
    id: listViewAll
    width: 300
    height: 380
    model: allListsModel
    delegate: contentOneLine
    clip: true

        Component {
            id: contentOneLine
            ListItem {
                height: ListView.view.orientation == ListView.Horizontal ? parent.height : 60
                width: ListView.view.orientation == ListView.Horizontal ? 100 : parent.width
                id: listitem
    
                ListItemText {
                    id: listitemtext
                    height: 23
                    text: type
                }
                CheckBox {
                    id: checkBox
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.right: parent.right
                    anchors.rightMargin: platformStyle.paddingLarge
                    checked: tick
                    onCheckedChanged:
                    {
                         console.log(listViewAll);
                         console.log(listViewAll.model);
                    }
                }
            }
        }
    }
    

    @

    The list model is implemented in cpp file:
    @
    ShoppingListModel::ShoppingListModel(QObject *parent)
    : QAbstractListModel(parent)
    {
    QHash<int, QByteArray> roles;
    roles[TypeRole] = "type";
    roles[TickRole] = "tick";
    setRoleNames(roles);
    }

    void ShoppingListModel::addShoppingList(const ShoppingList &shoppinglist)
    {
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_shoppinglists << shoppinglist;
    endInsertRows();
    }

    int ShoppingListModel::rowCount(const QModelIndex & parent) const {
    return m_shoppinglists.count();
    }

    QVariant ShoppingListModel::data(const QModelIndex & index, int role) const {
    if (index.row() < 0 || index.row() > m_shoppinglists.count())
    return QVariant();

    const ShoppingList &shoppinglist = m_shoppinglists[index.row()];
    if (role == TypeRole)
        return shoppinglist.type();
    else if (role == TickRole){
        return shoppinglist.tick();
    }
    return QVariant();
    

    }
    @

    I create the model and add it to the qdeclarative context by:
    @
    QmlApplicationViewer viewer;
    QDeclarativeContext *viewerCtxt = viewer.rootContext();
    ShoppingListModel model;
    ShoppingList sl1 = ShoppingList("Cleaning equipment");
    sl1.setTick(false);
    model.addShoppingList(sl1);

    ShoppingList sl2 = ShoppingList("For weekend");
    sl2.setTick(true);
    model.addShoppingList(sl2);
    viewerCtxt->setContextProperty("allListsModel", &model);
    @

    I want to update the model when the checkbox is checked or unchecked, but the row where I call

    listViewAll.model

    crashes the application.

    The application starts and shows the page correctly, so the model is found at that point. But why does calling the model again crash the whole thing? I would appreciate any help!



  • I guess the console.log() tries to convert model to string or call model's toString() like function, but there is no such function.
    I think you should not use console.log() to print the model



  • Is it possible your model is being destroyed from C++ (e.g. it is constructed on the stack and goes out of scope)?



  • I added to my model
    @
    void ShoppingListModel::testMe() const
    {
    qDebug() << "Testing model";
    }
    @
    and changed the onCheckChanged to
    @
    onCheckedChanged:
    {
    listViewAll.model.testMe();
    }
    @
    so it seems to me that the model is destroyed. But this is the first time I'm writing any C++ code so I don't understand why it would be destroyed.



  • [quote author="Sarry" date="1316500401"]... this is the first time I'm writing any C++ code so I don't understand why it would be destroyed.[/quote]
    Then this is the most probable reason.
    [quote author="mbrasser" date="1316476084"](e.g. it is constructed on the stack and goes out of scope)?[/quote]

    See "The stack and the heap":http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.