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. thread in QGraphicsScene

thread in QGraphicsScene

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 3.0k Views
  • 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.
  • LimerL Offline
    LimerL Offline
    Limer
    wrote on last edited by Limer
    #1
    MyScene *scene = new MyScene; // class MyScene inherite QGraphicsScene
    QGraphicsView *view = new QGraphicsView;
    view->setScene(scene);
    

    The scene has a function which is complex and time-consuming, so I want to create a therad to sovle the function. Because the function will use the member variables, so I can't isolate this function from the calss MyScene.

    There are two ways to create a thread.

    One, by inheriting QThread, but if I do so, the class MyScene will both inherite class QGraphicsScene and QThread. It's workable????

    Two, by inheriting QObject and use function moveToThread(), but the premise is scene has no parent. It's impossible, view will be its parent.

    Can someone give me some advice? How do I do it by using multiple threads?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why is your MyScene class inheriting from both QGraphicsScene and QThread ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      LimerL 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Why is your MyScene class inheriting from both QGraphicsScene and QThread ?

        LimerL Offline
        LimerL Offline
        Limer
        wrote on last edited by
        #3

        @SGaist Because I want to use thread.

        jsulmJ 1 Reply Last reply
        0
        • LimerL Limer

          @SGaist Because I want to use thread.

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

          @Limer said in thread in QGraphicsScene:

          Because I want to use thread

          Why?

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

          LimerL 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Limer said in thread in QGraphicsScene:

            Because I want to use thread

            Why?

            LimerL Offline
            LimerL Offline
            Limer
            wrote on last edited by
            #5

            @jsulm The scene has a function which is complex and time-consuming, so I want to create a therad to sovle the function. Because the function will use the member variables, so I can't isolate this function from the calss MyScene.

            jsulmJ 1 Reply Last reply
            0
            • LimerL Limer

              @jsulm The scene has a function which is complex and time-consuming, so I want to create a therad to sovle the function. Because the function will use the member variables, so I can't isolate this function from the calss MyScene.

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

              @Limer Does it really need to use member variables? It would be better to pass the values of this member variables to this function as parameters. I'm not completely sure, but most probably it is not allowed to access these classes from several threads (this is the case for widgets for sure).

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

              LimerL 1 Reply Last reply
              2
              • jsulmJ jsulm

                @Limer Does it really need to use member variables? It would be better to pass the values of this member variables to this function as parameters. I'm not completely sure, but most probably it is not allowed to access these classes from several threads (this is the case for widgets for sure).

                LimerL Offline
                LimerL Offline
                Limer
                wrote on last edited by
                #7

                @jsulm Yes, it have to use member variables.

                I‘ve thought about passing the values of this member too,,,but I want to know whether there is an anthor better solution.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  What are these member variables that you need to pass ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  LimerL 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    What are these member variables that you need to pass ?

                    LimerL Offline
                    LimerL Offline
                    Limer
                    wrote on last edited by
                    #9

                    @SGaist About 1000+ QGraphicsRectItems in a QList, and several variables(bool).

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by SGaist
                      #10

                      I don't see anything that you can't pass to your thread for processing provided that you don't modify a GUI element in it.

                      [edited SGaist]

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      LimerL 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        I don't see anything that you can't pass to your thread for processing provided that you don't modify a GUI element in it.

                        [edited SGaist]

                        LimerL Offline
                        LimerL Offline
                        Limer
                        wrote on last edited by
                        #11

                        @SGaist You mean I can pass these variables to thread?

                        This is my first try to use thread, so I don't know whether my solution is right or optimal.

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

                          Most anything related to QGraphicsView has to run in the main thread. Handing over any pointers to a class derived from QGraphicsItem to a thread is risky.

                          I find it better to completely separate the complex function from any GUI-related classes:

                          1. Extract the data you need from your GraphicsScene into a custom-made data class
                          2. Hand over that data class to a thread for processing
                          3. Extract the results and apply them to your scene.

                          If you process a list of items, and you need to apply a function to each item within the list, then you may want to take a look at QtConcurrentMap. It offers a much simpler interface for such use cases.

                          LimerL 1 Reply Last reply
                          1
                          • A Asperamanca

                            Most anything related to QGraphicsView has to run in the main thread. Handing over any pointers to a class derived from QGraphicsItem to a thread is risky.

                            I find it better to completely separate the complex function from any GUI-related classes:

                            1. Extract the data you need from your GraphicsScene into a custom-made data class
                            2. Hand over that data class to a thread for processing
                            3. Extract the results and apply them to your scene.

                            If you process a list of items, and you need to apply a function to each item within the list, then you may want to take a look at QtConcurrentMap. It offers a much simpler interface for such use cases.

                            LimerL Offline
                            LimerL Offline
                            Limer
                            wrote on last edited by
                            #13

                            @Asperamanca Thanks for your solution.

                            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