Define a QModelIndex, but not see this one when insert in a Qhash... why ?



  • i try to populate some:
    QHash<QModelIndex, int> like Classeurs.id_parent
    QMap<QModelIndex, int> like Classeurs.id
    QHash<QModelIndex, Classeur> like Classeurs.myclasseurs

    with this code:
    @
    void Classeurs_treeView::rowsInserted(const QModelIndex &parent,
    int start, int end) {
    QModelIndex new_row_idx = Classeurs.classeurs_model->index(start, 0, parent);
    int id = Classeurs.classeurs_model->index(start, 2, parent).data().toInt();
    int id_parent = Classeurs.classeurs_model->index(start, 3, parent).data().toInt();
    QString name = new_row_idx.data().toString();
    QString comment = Classeurs.classeurs_model->index(start, 1, parent).data().toString();
    Classeur *myclasseur = new Classeur(this, id, id_parent);
    myclasseur->setInfos(name, comment);
    Classeurs.my_classeurs .insert(new_row_idx, myclasseur); // here new_row_idx is different
    Classeurs.id .insert(new_row_idx, id); // and here... different
    Classeurs.id_parent .insert(new_row_idx, id_parent); /// and here... different
    Classeurs.index_from_id .insert(id, new_row_idx); // also here...
    Classeurs.index_from_id_parent.insert(id_parent, new_row_idx); // and also here
    emit add_classeur(parent, start, end);
    }
    @

    but...
    in my debugger, i can see that (directly at first insertion) QModelIndex of each QHash / QMap are not the same... (but inside each, model (var "m") point on the same model adress of my QtreeView model.
    I think there is something i not understand.
    Why "new_row_idx has not the same value QModelIndex inside each Hash/Map ?



  • Don't store a QModelIndex. A QModelIndex is not valid for longer than the method call you got it from.

    If you need to keep model indices over time, use a QPersistentModelIndex instead.



  • ok, i answer myself (and maybe this can help some other beginners)...
    on irc some gguys explained me that QModelIndex coudl not be used like this because he just be construct for use one time... so each time, he change adress.

    so... impossible to use it this way. In fact the correct design patern with Qt is to use the "model" only (so... QStandardItemModel in this case).

    And then, i think i need absolutly to do my own implementation of this model to have functions i need, because i can not use data storage this way.

    thanks for reading and welcome for all other ideas to do a best design patern for use with Qt tools.



  • well.. so it can be possible by QPersistentModelIndex (thanks... to teach me that).

    But Andre, what is your opinion:
    1 go by QPersistentModelIndex
    2 solution 1 is a really bad design and then it is a better way to construct my own custom model

    ?

    consider that my datas are not big and need to be able to be modified/deleted/added easily by the user and store datas at end time in a database.



  • I always suggest to use the Qt model/view classes only as an adaptor for your own application specific data store. They should not be used as your main API for your own application specific data needs, but only as an interface that you can use with the view classes and filters.

    So, don't model your data store on top of something like a QStandardItemModel. Instead, create a good data store for your application needs yourself, and either build a QAbstractItemModel (or QAbstractTableModel or QAbstractListModel) derived class that only uses your data store to present your application data in a view, or give your data store class a method that just returns a QAbstractItemModel.

    Edit: so go with solution 2). QPersistentModelIndex has its uses for tricks with special proxies for instance, but you really should leave the model/view API for just that: being a standardized data interface between models and views.



  • So ok, if i well understand, the good design for a QTreeView would be to customize a QAbstractItemModel if the QStandardItemModel is not enough for the job i need with the QTreeView.

    Also, i need to store datas contain by the model inside a specific class (like Classeur in this situation).

    So it look like i do a Model/View/Controller, by add the Controller for my data to the original design of Qt (the Classeur class is like a Controlle rin fact...). So... i will have the view specific class (for each QTreeView), a model class for customize each view... and a "storage/controller class" for each object specific data.

    Ok... so, wich one has to contain the part of store datas in the database for do good design ? consider i don't want to store each time i change a data directly, but at the "end time" when i click the "OK/accept()" button of the QDialog.

    Also, each time i insert data in a field (inside a QTreeView or QTextEdit or QLineEdit), i need to check if it is autorized (not a double exist allready inside the database, safety entry, etc...). And then, there is not only one table (for sure), but many of them who are logicaly linked together (foreign keys).

    My difficulty who, little by little, pushed me to do this wrong design was the fact that i need many time to have the last index (and datas content linked too...) inserted.
    Also, for a tree stored inside a database table, i need self reference parent ids... and then, when i delete one, i need to do some "wash"... for me, all of this is not so clear to find the best design to do it well.

    also, i see in my application that i have sometime some "freeze" when i click inside the QTreeView... so sure, my design is wrong and unstable.

    i'm a little bit afraid about multiplications of class and files (i have allready 26 cpp files and it is just the start of the application)... is it possible also to store them inside some specific directory with QtCreator (excuse for my newbies questions... but, i'm really a "newbie one") ?



  • Andre (or someone other), could you give me a link for learn to use QAbstractItemModel well (so... with some exemples include) ?

    I would like to use QAbstractItemModel for do my own models who all of them include SQL query database managment between the database and a QTreeView.

    I think QAbstractItemModel would be the good choice for start... but i'm not sure too...

    for exemple, include in my own customized model a "setQuery(const QList<QVariant> &fields, const QString &table)" and for exemple an other "setQueryOrder(const QList<QVariant> &list_fields, const order &type) where order is an enum...

    and then implement a customized QtreeView include a function member "refresh()" for populate the QTreeView.


Log in to reply
 

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