Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calling ListView's model crashes the application
Forum Updated to NodeBB v4.3 + New Features

Calling ListView's model crashes the application

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 4 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sarry
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex2202375
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbrasser
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sarry
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            [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/.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved