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; .....) ........... 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!
-
Hi
you should show definition of
lines_vector
and line_structHow 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.htmlU 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; struc_line line_struct std::vector<struc_line> lines_vector;
How can I use Qmap key for doing that?
Thanks again!
-
Hi
Read doc on how to use:
http://doc.qt.io/qt-5/qmap.html#detailsSomething 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); -
@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. -
@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 -
-
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(...) 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
-
yes
you must loop over list and check eachQList<QGraphicsItem *> selList=ui->scene->selectedItems();
for (int c=0; c < selListt.size(); c++) {
QGraphicsLineItem *found= LinesMap.value(selList[c], NULL);
if (found) .....
} -
@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
-
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. -
super:)
good work.
please mark as solved.