Qt World Summit: Register Today!

Custom model system - error when replacing root item, no debug info :- (

  • Hey

    This is a little tricky, as I'm quite limited in what source I can post and the fact that entire qabstractitemmodel/item/etc were written and no standard qt model objects are used...

    In any case, the setup is as follows:


    I have a case where I want to display 2 qtreeviews, and display content of item from TreeA in treeB.
    I have "2" models. modelA for treeA, this one remains unchanged. modelB I replace this model rootItem all the time - this is where error "happens"?
    Each treeItem, has a "childModel" - item, that contain its own data. Kinda dual data structure. Essentially

    class treeItem: {
        treeItem *modelItem; // << this is another item inside this item
        inline treeItem*getChildModel(){return modelItem;}

    The idea is, whenever I click on any of items in treeA, I get its item->getChildModel item and I set that as a root item on modelB. By replacing rootItem I replace entire data structure in modelB thus I can quickly change data? I think...

    In any case, this works, if I click on any item on treeA, treeB content changes - all great. The problems starts if for example... I edit text in treeB, it changes as it should and data appear to be "updated". But here is the kicker... when I change item in treeA
    It now broke down and I can't change items in treeA anymore... just by running test in VS instead of qtCreator.

    In any case I have a sea of Qt5Cored.dll/Qt5Widgetsd.dll errors and no symbols loaded from xxx...

    Maybe I need to emit a signal before changing rootItem in my modelB ?

    How can I fix that? I'm lost :- (

    Yep it feels like its down to replacing the root item of tree model and I guess qt crashes in the thread or something like that when he loses root item. Perhaps I should mutex it...mhmm

    Soo it seems that QMutex & and it works ?...mhmmmm Feels like I could us a signal that would tell Qt that I'm about to mess with model root...

  • Lifetime Qt Champion


    You have written one very important word: thread. If you are using threads then access to all resources that can be accessed from multiple threads must be protected. Qt won't do that for you, it's your responsibility.

    As for signalling you are about to modify your model, take a look at the various beginXXX/endXXX methods in QAbstractItemModel. In your case, beginReset/endReset are likely the pair you are looking for. Note that these methods don't replace proper access protection when using multiple threads.

  • Hey

    Well I didnt start multiple threads, I think they are being run/managed by Qt. I recon that while QTreeView::DrawRows was being run & I change root item in my model, then that caused error & crash. I did look at begin/end. Just did not find any that sounded like... aboutToChangeRootItem or something like that. I found one "private" signal but I had no idea what to do with it.

  • Lifetime Qt Champion

    As I suggested, beginResetModel and endResetModel are what you are looking for. it's what usually goes with "i'm going to nuke everything".

Log in to reply