[Solved] Safeguarding selectedItems() and items()
-
I have two asynchronous function calls happening on a customized QGraphicsScene. One function call removes an item and the other gets a listing of all the items. Sometimes these function calls will happen at the same time and when that happens, it corrupts the listing of all the items. Is there any way to safeguard the list of the items? I've tried a QMutexLocker, but that does not seem to be helping.
void myScene::theCorrupter(QGraphicsItem* item) { removeItem(item); } QMutex mutex; void myScene::listOutItems() { QMutexLocker locker(&mutex); //this seems to have no effect if(!items().isEmpty()) { foreach(QGraphicsItem* item, items() ) {//if the theCorrupter function is called during life of this function // it will cause a crash qDebug()<<"pos = "<<item->pos(); } } }
-
You have to add
QMutexLocker locker(&mutex);
to theCorrupter():
void myScene::theCorrupter(QGraphicsItem* item) { QMutexLocker locker(&mutex); removeItem(item); }
-
@jsulm Thanks....that worked!
-
@w.h. Glad to hear it works :-)
-
Good for you, but keep in mind that GraphicsView is not meant for multithreading.