Important: Please read the Qt Code of Conduct -

Remove items selected from scene in a dynamic array

  • 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; .....)
              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!

  • Lifetime Qt Champion

    you should show definition of
    and line_struct

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

    else what about using a QMap?

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

  • @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 line_struct
    std::vector<struc_line> lines_vector;

    How can I use Qmap key for doing that?

    Thanks again!

  • Lifetime Qt Champion

    Read doc on how to use:

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

    // add it
    line = scene->addLine(...)

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

  • @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.

  • Lifetime Qt Champion

    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 ?

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

  • Lifetime Qt Champion

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

  • @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(...)
    //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

  • Lifetime Qt Champion

    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) .....

  • @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?

  • Lifetime Qt Champion

    yes, your vector should be replaced with map.

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

  • Lifetime Qt Champion

    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.

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

  • Lifetime Qt Champion

    good work.
    please mark as solved.

Log in to reply