Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Is it possible to notify items in the model without signals?



  • Sorry if it's wrong place to ask, so my problem is I have a model which I derived from QAbstractListModel.
    which contains a list of objects with id. My application listens for a new data coming from a server so when new
    data is coming I loop over my list compare ids to receive object index, update the object and emit dataChanged signal to update a view.
    I have lots of these objects so I don't want to derive from QObject. I can use dict or something to map ids to indexes but
    I fill that I doing something wrong and there should be an easier solution to this problem


  • Moderators

    Hi @arguskso, could you decribe what you mean by "notify items in the model"?

    I have lots of these objects so I don't want to derive from QObject

    Indeed, there is usually no need for objects that represent model data to inherit QObject.



  • @JKSH
    Maybe notify is a wrong word, imagine I have several objects in a list when a new data is coming I somehow update
    my object with a corresponding id in my model, but to update my view I have to know the index of the updated element to
    call dataChanged signal. Is there a better way to find index than just iterating and comparing ids?



  • @arguskso
    You have to generate the necessary/correct row/columns from model for the dataChanged signal. That's the whole basis models & views with QIndex() work in Qt. Whether there is a "a better way to find index than just iterating and comparing ids" is up to your design in your model. Yes, " I can use dict or something to map ids to indexes " is the way to make it faster.



  • @JonB
    Objects can also be inserted at random place so updating indexes in a dict is not a solution,
    Now I thinking that delegates can send signals and know their index so I can pass id to the delegate.
    Connect all delegates with a signal and slot mechanism to my model (which is QObject) and when new
    data is coming I can emmit signal with an id and all delegates will receive it and one with the corresponding id will send one more
    signal back to the model with it's index in the model.
    But I fill like this is also not a very clean solution. Is there nothing well established for this kind of problem?


  • Lifetime Qt Champion

    @arguskso said in Is it possible to notify items in the model without signals?:

    when new
    data is coming I can emmit signal with an id and all delegates will receive it and one with the corresponding id will send one more
    signal back to the model with it's index in the model.

    Delegates are used for displaying, they don't know anything about the internal data structure and don't have really access to anything else except it's own index. This can't work. You have to do it the way @JonB suggested to you.