Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Links between items in QTreeItemView
Forum Updated to NodeBB v4.3 + New Features

Links between items in QTreeItemView

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 216 Views
  • 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.
  • U Offline
    U Offline
    user931357
    wrote on last edited by
    #1

    Hello. I would like to have links between items in a tree so that when I change something in one branch, changes should become immediately visible in a linked branch. Please see the picture below for an example:
    d4b07243-6b75-4040-b65a-e482525b2526-image.png
    This tree can have hundreds of thousands of items and several links to the same item should be possible.
    I have a custom implementation of the model (inherits from QAbstractItemModel) and items. I use this method to insert children (I took the idea from Qt “Editable Tree Model” example):

    // Count – how many empty children I would like to insert
    bool SqlTreeItem::insertChildren(int position, int count, int columns, int childId) {
        if (position < 0 || position > childItems.size())
            return false;
        for (int row = 0; row < count; ++row) {
            QVector<QVariant> data(columns);
    
            SqlTreeItem *item = new SqlTreeItem(data, childId, this);
            childItems.insert(position, item);
        }
        return true;
    }
    

    When I need to create a link, I do that like this:

    bool SqlTreeItem::linkChildren(int position, SqlTreeItem *to) {
        if (position < 0 || position > childItems.size())
            return false;
    
        SqlTreeItem *item = to;
        childItems.insert(position, item);
    
        return true;
    }
    

    Thus, when I want to insert a link, I just insert a pointer of the existing branch. I didn’t find anything in the documentation about this way but I still decided to try. It works fine sometimes, but sometimes does not or works incorrectly.
    Could somebody tell me, what could be the esiest way and what could be the correct way to implement this?

    VRoninV 1 Reply Last reply
    0
    • U user931357

      Hello. I would like to have links between items in a tree so that when I change something in one branch, changes should become immediately visible in a linked branch. Please see the picture below for an example:
      d4b07243-6b75-4040-b65a-e482525b2526-image.png
      This tree can have hundreds of thousands of items and several links to the same item should be possible.
      I have a custom implementation of the model (inherits from QAbstractItemModel) and items. I use this method to insert children (I took the idea from Qt “Editable Tree Model” example):

      // Count – how many empty children I would like to insert
      bool SqlTreeItem::insertChildren(int position, int count, int columns, int childId) {
          if (position < 0 || position > childItems.size())
              return false;
          for (int row = 0; row < count; ++row) {
              QVector<QVariant> data(columns);
      
              SqlTreeItem *item = new SqlTreeItem(data, childId, this);
              childItems.insert(position, item);
          }
          return true;
      }
      

      When I need to create a link, I do that like this:

      bool SqlTreeItem::linkChildren(int position, SqlTreeItem *to) {
          if (position < 0 || position > childItems.size())
              return false;
      
          SqlTreeItem *item = to;
          childItems.insert(position, item);
      
          return true;
      }
      

      Thus, when I want to insert a link, I just insert a pointer of the existing branch. I didn’t find anything in the documentation about this way but I still decided to try. It works fine sometimes, but sometimes does not or works incorrectly.
      Could somebody tell me, what could be the esiest way and what could be the correct way to implement this?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @user931357 said in Links between items in QTreeItemView:

      but sometimes does not or works incorrectly.

      Can you be more specific about what the problem is?

      My first thoughts are:

      • When inserting children you are not signaling it (beginInsertRows/beginInsertRows)
      • Run your model alongside the model test. The amount of problems it spots is invaluable

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2

      • Login

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