Move item Line with Line Edit.
-
Hi everyone.
First of all thanks for helping me.I have some LineItems in a Scene represented by QGraphicsView.
I would like to add 4 LineEdit and one button.The idea is the next:
the Line Edits are: right, left, up, down.
The button is: move lines.If the user selects and item and put for example: 3 in right and 0 in the left, up and down. The line selected is moved 3 coordinates in X to the right.
I thought that I can do that If I can access to coordinates of selected items and then just add the number of user puts in LIneEdit.
but how can I access to that¿
I add lines with:
line = scene->addLine(x_start,y_start,x_end,y_end, red);
Thanks a lot!
-
This post is deleted!
-
hi
"but how can I access to that¿"I assume you mean the line?
If selected then u can get it via
http://doc.qt.io/qt-5/qgraphicsscene.html#selectedItemsNote: that you might need to make the item selecteable
http://doc.qt.io/qt-5/qgraphicsitem.html#setFlags
QGraphicsItem::ItemIsSelectable -
@mrjj Thanks for answering.
Yes, the line selected or lines selected.
But I need the coordinates of the line (x_start, y_start, x_end,y_end)
I did this:QList<QGraphicsItem*>a; a = scene->selectedItems(); int num_item_selected; num_item_selected = a.size(); //Num of items selected. for (int i=0; i<num_item_selected; i++) { QRectF itemBR = a.value(i)->boundingRect(); QPointF sceneBR = a.value(i)->scenePos(); //std::cout << sceneBR << std::endl; }
but I can not get the coordinates...
-
It seems fine
Did you read about
http://doc.qt.io/qt-5/graphicsview.html#the-graphics-view-coordinate-systemscenePos() is correct so you must be getting other values than
expected?If you cast to an actual QGraphicsLineItem
QGraphicsLineItem * line=qobject_cast<QGraphicsLineItem * >(a.value(i));
if ( line ){
http://doc.qt.io/qt-5/qgraphicslineitem.html#line
}
Maybe those values are the ones you expect ? -
@mrjj Yes, the scenePost() retunr always 0,0 ... It does not matter what line I select, the value is always 0,0 ...
When I write your code it return me an error which is:
error: no matching function for call to 'qobject_cast(QGraphicsItem*)'
QGraphicsLineItem * linea =qobject_cast<QGraphicsLineItem * >(a.value(i));
^QGraphicsLineItem * linea =qobject_cast<QGraphicsLineItem * >(a.value(i)); if (linea) { linea->line; }