qgraphicsscene remove item but sometime still in view
-
Ok,I understood.Sorry for that.
Here is a simple example://click a button to switch line and run below code // a QGraphicsView scene()->addItem(m_chart); //replace a new line QLineSeries * tmp = static_cast<QLineSeries *>(m_chart->series()[0]); QList<QPointF> dataPointList; //set new axis range m_chart->axisX()->setRange(0,dataVec[dataVec.size()-1].x()); m_chart->axisY()->setRange(-qAbs(yMax)*1.2f, qAbs(yMax)*1.2f); tmp->replace(dataPointList); //delete last line a flag(circle)item in the qchart ,m_calloutsList is keep flags list for(auto& item : m_calloutsList[nDataIndex]) { //item.prepareGeomtryChange //try it not work scene()->removeItem(item); } //add current line flag item for(auto& item2 : m_calloutsList[nDataIndex]) { //item2.prepareGeomtryChange //try it not work scene()->addItem(item2); }
-
Please provide a minimal compilable example that shows the behavior.
-
@SGaist OK,I upload a minimal compilable example for github
github linkIs it okay? It base on qt example callout
-
try to After
remove the item from your qgraphicsscene
scene()->removeItem(YOURITEM_OBJECT); ui->graphicsview->viewport()->update(); ui->graphicsview->update(); ui->graphicsview->show();
-
@SGaist Having a question regarding calling hide cause i encounter similar problems.
In my scene i want to add some items when another item is clicked, but when i unselect the other items the items added shall be removed aswell.NOW as i figured when just calling remove onto the other items the programm crashes cause there is still the flag "isVisible" given back on remove.
Is it in this case fine to just case setVisible(false) and than scene()->removeItem(), because than it don't crashes. But im not sure if thats proper way to do it or is it just a whacky fix.
Here is my code that draws the items when the Owner item is clicked:
void ITMHandleDraw::drawHandlesIfNecessary() { //qDebug() << "DrawHandles" << ShouldDrawHandles << ownerItem->isSelected(); if(!ownerItem){ qWarning() << "ITMHandleDraw : No ownerItem set. Not drawing any\ handle. Please call setOwnerItem on your ITMHandleDraw subclass"; return; } if(ownerItem->isSelected()){ drawHandles(); }else if(!ownerItem->isSelected()){ handlesAddedToScene = false; // Ensure handles are removed from the scene before deletion //Remove the handles foreach (ITMHandleLogic * ITMHandleLogic, handleList) { // ITMHandleLogic->setVisible(false); ITMHandleLogic->scene()->removeItem(ITMHandleLogic); qDebug() << ITMHandleLogic->isVisible(); } qDeleteAll(handleList); handleList.clear(); } }
-
@StudentScripter said in qgraphicsscene remove item but sometime still in view:
foreach (ITMHandleLogic * ITMHandleLogic, handleList) {
Is this your real code?! Because it can't even compile.
I also don't get what this line is supposed to do:ITMHandleLogic->scene()->removeItem(ITMHandleLogic);
-
@jsulm said in qgraphicsscene remove item but sometime still in view:
foreach (ITMHandleLogic * ITMHandleLogic, handleList) {
Is this your real code?! Because it can't even compile.
Don't know if you maybe thought this was a
for
, but it's aforeach
and compiles fine? As does e.g.QList<QString *> QStringList; foreach (QString * QString, QStringList) {
Unfortunately the OP has a predilection for naming variables identically to their types, which is not helpful but (at least in this case) works fine.
I also don't get what this line is supposed to do:
ITMHandleLogic->scene()->removeItem(ITMHandleLogic);
Given
ITMHandleLogic
which is a pointer to aQGraphicsItem
this gets to theQGraphicsScene
from it viaQGraphicsItem::scene()
to callQGraphicsItem::removeItem(ITMHandleLogic)
. Alternatively the OP could just have calleddelete ITMHandleLogic
to delete and remove (per Qt internal code forQGraphicsItem
destructor which removes from scene). -
@StudentScripter said in qgraphicsscene remove item but sometime still in view:
calling remove onto the other items the programm crashes cause there is still the flag "isVisible" given back on remove.
Is it in this case fine to just case setVisible(false) and than scene()->removeItem(), because than it don't crashes.
First, why do you care about
isVisible()
orsetVisible()
at all in your situation? Once youremoveItem()
theQGraphicsItem
will no longer be shown on the scene/view so it's not relevant.But given that you want to use it. After
removeItem()
the item no longer belongs to the scene in any way. I would guess thatQGraphicsItem::visible()
needs to access the scene from the item in order to do whatever calculations are required for "visibility". But for a removed item the scene is probably "undefined" (or maybenullptr
) and hence it "crashes". Do not call any methods on aQGraphicsItem
once it has been removed from the scene. If you really want to set/test visibility do so beforeremoveItem()
.