I am using Qt to draw out some data visualisations. I have lines which indicate certain important points along a timeline, but I would like to be able to remove these lines from view so that the information underneath is more clearly visible.
I understand how to remove them from sight, but the problem is how to find which QGraphicsItems in the scene’s view are Lines and which are not.
I have tried using
try {
qgraphicsitem_cast<QGraphicsLineItem>(scene->items()[i]);
} catch (...) {
}
But this doesn’t even compile. I tried checking the output of qgraphicsitem_cast() to see if was 0, but the compiler didn’t like that, either.
This is my most current code:
void Plotter::showHideLines() {
int i;
QGraphicsLineItem l;
for (i = 0; i < scene->items().count(); i++) {
try {
qgraphicsitem_cast<QGraphicsLineItem>(scene->items()[i]);
scene->items()[i]->setVisible(!scene->items()[i]->isVisible());
} catch (...) {
}
}
}
Ignore l, I didn’t bother to delete it after trying something else.
I am pretty new to Qt, I have just been learning it over the last few days. Can anybody help?
I wouldn’t advise iterating through every scene item, just to hide a certain type as things will get slow when the number of items gets large.
Instead, whenever a line is created, add it to a list. When they need to be hidden, iterate through list and hide them. It takes slightly more memory, but is much faster, safer, and requires less coding.