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. Select Item in QTreeView by QGraphicsItem
QtWS25 Last Chance

Select Item in QTreeView by QGraphicsItem

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 2.5k 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.
  • M Offline
    M Offline
    monkeyDancer
    wrote on last edited by
    #1

    I have a QAbstractItemModel shown in a QTreeView and i have QGraphicsItems in a QGraphicsScene.

    A pointer to a GraphicsItem is stored in the AbstractItemModel so clicking an item in the tree is selecting the associated item in the scene.

    I would like to archieve that clicking a Item in the scene will select the associated item in the tree.

    The tree is not static - adding and removing items will happen regularly.

    I have not found a solution. Assungming that the model consits of several thousand items, workarunds like "testing for equility in loops" etc. are not applicable.

    Maybe there is some tricky stuff i have missed?!

    best regards

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Subst27
      wrote on last edited by
      #2

      Hi!

      QVariant QGraphicsItem::​data(int key) const
      to stored pointer to Item ?

      or internal QHash <graphicsItem, item> ?

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

        I have already a pointer to the node in my GraphicsItem.

        The problem is to determine the QModelIndex through the model and the parent-child relationship.

        If i have this QModelIndex i could create a QItemSelectionModel and set it in the view.

        Having the node gives me the depth of the item in the tree by
        looping through the parent till the node is invalid (=root).

        But i dont have the indices in the child container. With this indices i could create a QModelIndex determined by parent, row and column.

        Storing the index or comparing Qbjects or using QHash will not be applicable concerning the high amount and frequently adding and removing of items.

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

          conceptually, you want to have access to nodes by parent-child in both direction?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            monkeyDancer
            wrote on last edited by
            #5

            The access to the nodes in the abstractitemmodel is not the problem.

            An Example:
            A node has the two members
            Node parent;
            QList<Node
            > children;

            Lets say we have a child in the list at index 3. The child does not know about the index. It just knows about its parent.

            Now i could store the index of the child in the node itself. But that would force me to update the indices if a sibling is removed or added.

            If an item on pos 2 is removed and it has 4000 sibling i have to update many indices.

            But maybe i missed something important ?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Subst27
              wrote on last edited by
              #6

              In what purpose you want to get index of item?
              When you click on it and select the corresponding graphics item?

              Other purposes?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                monkeyDancer
                wrote on last edited by
                #7

                Clicking a item in the tree to get the graphics item is no problem cause a pointer to the graphics item is part of the userData of the node.

                The other way round.

                So far i can click the graphicsItem and get the corresponding node in the model through a direct pointer (graphicsItem->Node).

                Selecting the item in the VIEW! is the problem.
                Or more precisely - creating a QModelIndex in the model without knowing about the row that the item is positioned.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Subst27
                  wrote on last edited by
                  #8

                  and reassign internal data as Row is slow because so many items.

                  as cardinal varant - use in memory sqlite

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    monkeyDancer
                    wrote on last edited by
                    #9

                    I think using an int as member of a node to determine the index the item has (in the parent's list<node> children) is not the badest solution.

                    Implementing the increase or decrease of the indices in the model itself. In the methods "insertRows" and "removeRows" i have direct access to the nodes.

                    So no need to set data through the setData method.

                    But its kind of ugly. Even having a pointer to a node from outside the model is breaking the interface.

                    But ...
                    you have to break an egg to make an omelette.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Subst27
                      wrote on last edited by
                      #10

                      May be you can make some correct of this 'ugly' by reimplementing RESET.
                      It's slow but...

                      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