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. Remove items selected from scene in a dynamic array
Qt 6.11 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.0k 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 Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    Hello everyone.
    First of all thanks for helping me and reading this post.

    I have some items in a scene and in a dynamic array. I save them like this:

    for (i=0; .....)
    ...........
               lines_vector.push_back(line_struct);
              line = scene->addLine(...)
    

    Now i would like to remove items selected by users.
    I remove these items in a scene like this when user press a button:

    void MainWindow::on_RemoveLines_clicked()
    {
        qDeleteAll(scene->selectedItems());  //We delete items selected.
    }
    

    But i do not know how to remove item selected in dynamic array...

    Any ideas? Thanks!

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

      Hi
      you should show definition of
      lines_vector
      and line_struct

      How fill you find the right line_struct from the scene->selectedItems() ?
      start x,y ?

      else what about using a QMap?
      http://doc.qt.io/qt-5/qmap.html

      U then store line* , line_struct
      Then you can always find the line_struct again using the line * as key.

      AlvaroSA 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        you should show definition of
        lines_vector
        and line_struct

        How fill you find the right line_struct from the scene->selectedItems() ?
        start x,y ?

        else what about using a QMap?
        http://doc.qt.io/qt-5/qmap.html

        U then store line* , line_struct
        Then you can always find the line_struct again using the line * as key.

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

        @mrjj Hello! Thanks for answering!
        This is the definitions:

        typedef struct { //We define the line structure which has always six elements (x_start, y_start, x_end, y_end, psi,r)
            double x_start;
            double x_end;
            double y_start;
            double y_end;
            double psi;
            double r;
        }struc_line;
        
        struc_line line_struct
        
        std::vector<struc_line> lines_vector;
        
        

        How can I use Qmap key for doing that?

        Thanks again!

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

          Hi
          Read doc on how to use:
          http://doc.qt.io/qt-5/qmap.html#details

          Something like:
          in class, add
          QMap<QGraphicsLineItem *, line_struct> LinesMap;

          // add it
          line = scene->addLine(...)
          LinesMap[line]=line_struct;

          // find it again
          QGraphicsLineItem *found= LinesMap.value(SELECTED_LINE, NULL);
          if (found) // it has it.
          LinesMap.remove(SELECTED_LINE);

          AlvaroSA 1 Reply Last reply
          1
          • mrjjM mrjj

            Hi
            Read doc on how to use:
            http://doc.qt.io/qt-5/qmap.html#details

            Something like:
            in class, add
            QMap<QGraphicsLineItem *, line_struct> LinesMap;

            // add it
            line = scene->addLine(...)
            LinesMap[line]=line_struct;

            // find it again
            QGraphicsLineItem *found= LinesMap.value(SELECTED_LINE, NULL);
            if (found) // it has it.
            LinesMap.remove(SELECTED_LINE);

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

            @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 1 Reply Last reply
            0
            • 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 Extensions
                                  • Unsolved