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. Best way to handle drawing from thread

Best way to handle drawing from thread

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.4k 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.
  • H Offline
    H Offline
    HalfTough
    wrote on last edited by HalfTough
    #1

    I have a thread running. What I want to do, is draw some images in my QWindow depending on what's going on in this thread.

    So I put QGraphicsView in my QWindow, QScene inside of that QGraphicsView and finally I added some QGraphicsObjects to the scene. This part works nicely.

    Now, in my thread, I do some math and then trying to run something like object1->setTransform(), where object1 is pointer to QGraphiscObject that was added to QScene. (No semaphores or other forms of memory protection for now.) And it kinda works, but I keep getting QObject::startTimer: Timers cannot be started from another thread warning during the runtime. On the top of that, transformed object is flickery sometimes (I change it at least 30 times/s).

    My question is: What is the proper way to do that? Is QGraphicsView right tool for the job? I'm thinking of adding OpenGl in the future. How does it compute?

    jsulmJ 1 Reply Last reply
    0
    • H HalfTough

      I have a thread running. What I want to do, is draw some images in my QWindow depending on what's going on in this thread.

      So I put QGraphicsView in my QWindow, QScene inside of that QGraphicsView and finally I added some QGraphicsObjects to the scene. This part works nicely.

      Now, in my thread, I do some math and then trying to run something like object1->setTransform(), where object1 is pointer to QGraphiscObject that was added to QScene. (No semaphores or other forms of memory protection for now.) And it kinda works, but I keep getting QObject::startTimer: Timers cannot be started from another thread warning during the runtime. On the top of that, transformed object is flickery sometimes (I change it at least 30 times/s).

      My question is: What is the proper way to do that? Is QGraphicsView right tool for the job? I'm thinking of adding OpenGl in the future. How does it compute?

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

      @HalfTough Never access your UI from other threads! This is not supported. So, your other thread should not draw anything, instead it should notify the UI thread (for example using signals/slots) about the calculated changes. The UI thread then draws what needs to be drawn.

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

      JonBJ 1 Reply Last reply
      2
      • jsulmJ jsulm

        @HalfTough Never access your UI from other threads! This is not supported. So, your other thread should not draw anything, instead it should notify the UI thread (for example using signals/slots) about the calculated changes. The UI thread then draws what needs to be drawn.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @jsulm said in Best way to handle drawing from thread:

        @HalfTough Never access your UI from other threads! This is not supported. So, your other thread should not draw anything, instead it should notify the UI thread (for example using signals/slots) about the calculated changes. The UI thread then draws what needs to be drawn.

        Purely out of idle/malicious curiosity --- I have no usage case --- but would this still apply if you suspended the UI thread while drawing from the other thread (then exited that thread and restarted UI thread)? i.e. is the reason contention issues, or does the UI thread have local stuff which is needed but only it can access? I imagine the latter (as well as the former, I suppose), but just interested to hear.

        jsulmJ 1 Reply Last reply
        0
        • JonBJ JonB

          @jsulm said in Best way to handle drawing from thread:

          @HalfTough Never access your UI from other threads! This is not supported. So, your other thread should not draw anything, instead it should notify the UI thread (for example using signals/slots) about the calculated changes. The UI thread then draws what needs to be drawn.

          Purely out of idle/malicious curiosity --- I have no usage case --- but would this still apply if you suspended the UI thread while drawing from the other thread (then exited that thread and restarted UI thread)? i.e. is the reason contention issues, or does the UI thread have local stuff which is needed but only it can access? I imagine the latter (as well as the former, I suppose), but just interested to hear.

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

          @JNBarchan I don't know and would never do something like this.

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

          JonBJ 1 Reply Last reply
          0
          • jsulmJ jsulm

            @JNBarchan I don't know and would never do something like this.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @jsulm Lol, OK, I was just interested :)

            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