Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Remove items selected from scene in a dynamic array
Qt 6.12 LTS is out! See what's new in the release blog

Remove items selected from scene in a dynamic array

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 2 Posters 9.1k Views 1 Watching
  • 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.
  • AlvaroSA AlvaroS

    @mrjj thanks a lot for answering.
    i am gonna to try that.
    And I am thinking about someting.
    When I add line to the scene I do that in a for structure.
    Is there any way to get the iterator of item selected ?
    Because if i get that I can remove the line in the dimanyc array lines_vector if I now the iterator where I have to remove.

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #6

    @AlvaroS
    Hi
    I dont know what you mean by
    "Is there any way to get the iterator of item selected ?"

    You mean scene->selectedItems() ?
    QList<QGraphicsItem *> QGraphicsScene::selectedItems() const
    Its a list ?

    AlvaroSA 1 Reply Last reply
    0
    • mrjjM mrjj

      @AlvaroS
      Hi
      I dont know what you mean by
      "Is there any way to get the iterator of item selected ?"

      You mean scene->selectedItems() ?
      QList<QGraphicsItem *> QGraphicsScene::selectedItems() const
      Its a list ?

      AlvaroSA Offline
      AlvaroSA Offline
      AlvaroS
      wrote on last edited by
      #7

      @mrjj When I do for structure I save the line in the scene and in the lines_vector in the same iterator.
      So the line in 4 position in scene is the same position than the line in 4 position in lines_vector.
      So when I select for example one line in the scene, for example line 4, I have just to remove the line in position 4 in lines_vector.
      So is ther any way to get the position vector of line that user selected in the scene

      mrjjM 1 Reply Last reply
      0
      • AlvaroSA AlvaroS

        @mrjj When I do for structure I save the line in the scene and in the lines_vector in the same iterator.
        So the line in 4 position in scene is the same position than the line in 4 position in lines_vector.
        So when I select for example one line in the scene, for example line 4, I have just to remove the line in position 4 in lines_vector.
        So is ther any way to get the position vector of line that user selected in the scene

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #8
        • in the same iterator.
          Iterator is a helper class. Do you mean same index?
          So first line and first struc_line but is at [0] in both lists?

        • So is ther any way to get the position vector of line that user selected in the scene
          The list contains one or more selected items.
          Its not in the order it was inserted. Just the order selected.
          So I dont think u can make a 1:1 map this way using selectedItems.

        AlvaroSA 1 Reply Last reply
        1
        • mrjjM mrjj
          • in the same iterator.
            Iterator is a helper class. Do you mean same index?
            So first line and first struc_line but is at [0] in both lists?

          • So is ther any way to get the position vector of line that user selected in the scene
            The list contains one or more selected items.
            Its not in the order it was inserted. Just the order selected.
            So I dont think u can make a 1:1 map this way using selectedItems.

          AlvaroSA Offline
          AlvaroSA Offline
          AlvaroS
          wrote on last edited by
          #9

          @mrjj Yes, index sorry.
          Okey so I can not do like that.

          I have tried to use QMap like:

          QMap<QGraphicsLineItem *, struc_line> LinesMap;
          
          //Adding lines to the scene and lines_vector
          for (int i=0; i<num_lines;i++)
                             line = scene->addLine(...)
                             LinesMap[line]=lines_vector[i];
          
          //Button to remove lines selected
          
           QGraphicsLineItem *found= LinesMap.value(ui->scene->selectedItems(), NULL);
          
          

          But it does not compile because there is a problem if users select more than one item because scene->selectedItems is a list

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #10

            yes
            you must loop over list and check each

            QList<QGraphicsItem *> selList=ui->scene->selectedItems();

            for (int c=0; c < selListt.size(); c++) {
            QGraphicsLineItem *found= LinesMap.value(selList[c], NULL);
            if (found) .....
            }

            AlvaroSA 1 Reply Last reply
            0
            • mrjjM mrjj

              yes
              you must loop over list and check each

              QList<QGraphicsItem *> selList=ui->scene->selectedItems();

              for (int c=0; c < selListt.size(); c++) {
              QGraphicsLineItem *found= LinesMap.value(selList[c], NULL);
              if (found) .....
              }

              AlvaroSA Offline
              AlvaroSA Offline
              AlvaroS
              wrote on last edited by
              #11

              @mrjj Okey and there I remove item of QMap but how can I remove item of lines_vector?
              Or you mean that I have to use QMap instead of std::vector?

              mrjjM 1 Reply Last reply
              0
              • AlvaroSA AlvaroS

                @mrjj Okey and there I remove item of QMap but how can I remove item of lines_vector?
                Or you mean that I have to use QMap instead of std::vector?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @AlvaroS
                yes, your vector should be replaced with map.

                AlvaroSA 1 Reply Last reply
                0
                • mrjjM mrjj

                  @AlvaroS
                  yes, your vector should be replaced with map.

                  AlvaroSA Offline
                  AlvaroSA Offline
                  AlvaroS
                  wrote on last edited by
                  #13

                  @mrjj I have tried to use QMap like you said but I get an error here:

                  error: no matching function for call to 'QMap<QGraphicsLineItem*, struc_line>::key(QGraphicsItem*&)'
                  QGraphicsLineItem *found=LinesMap.key(selList[c],NULL);
                  ^

                  You know why?

                  Thanks a lot again

                  1 Reply Last reply
                  1
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #14

                    yes,we store QGraphicsLineItem * in map and selectedItems is QGraphicsItem *

                    If you only do select "lines" then u can store it as QGraphicsItem * instead and
                    then no need for casting etc.

                    AlvaroSA 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      yes,we store QGraphicsLineItem * in map and selectedItems is QGraphicsItem *

                      If you only do select "lines" then u can store it as QGraphicsItem * instead and
                      then no need for casting etc.

                      AlvaroSA Offline
                      AlvaroSA Offline
                      AlvaroS
                      wrote on last edited by
                      #15

                      @mrjj Perfect! It solved using std::map!!!! Thanks a lot

                      1 Reply Last reply
                      1
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #16

                        super:)
                        good work.
                        please mark as solved.

                        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
                        • Unsolved