Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Safeguarding selectedItems() and items()

[Solved] Safeguarding selectedItems() and items()

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 964 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    w.h.
    wrote on last edited by w.h.
    #1

    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();
              }   
        }
    }
    
    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You have to add

      QMutexLocker locker(&mutex);
      

      to theCorrupter():

      void myScene::theCorrupter(QGraphicsItem* item)
      {
           QMutexLocker locker(&mutex);
           removeItem(item);
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        w.h.
        wrote on last edited by
        #3

        @jsulm Thanks....that worked!

        jsulmJ 1 Reply Last reply
        0
        • W w.h.

          @jsulm Thanks....that worked!

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @w.h. Glad to hear it works :-)

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            Good for you, but keep in mind that GraphicsView is not meant for multithreading.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved