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 have a QObject-derived class run stuff each frame (or in it's own thread)? [Qt/QML C++]
Forum Updated to NodeBB v4.3 + New Features

Best way to have a QObject-derived class run stuff each frame (or in it's own thread)? [Qt/QML C++]

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.3k Views 1 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.
  • K Offline
    K Offline
    katanaswordfish
    wrote on 4 May 2014, 00:35 last edited by
    #1

    Background: I have a custom QObject-derived class that wraps an SDL2 context for polling inputs for gamepad/joystick-like devices. After initializing SDL, each frame (or, maybe in a new thread) this class will need to (1) poll events from SDL's event queue and (2) process the returned events to determine what kind of event occurred. After processing events, I plan on emitting event-specific signals to be easily used by my other QObjects. I happen to be using QML within my project, but if I understand correctly, this shouldn't require QML interaction at all and is more of a general question about QObjects...

    So how can I poll/process/emit events each frame within my custom QObject subclass? Is there some kind of 'update' function that Qt calls for each QObject that I can overwrite? Does each object contain some kind of thread that runs independently that I can use? Or do I have to manually create my own thread in the QObject-derived class' constructor?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JKSH
      Moderators
      wrote on 4 May 2014, 04:21 last edited by
      #2

      Hi,

      Yes, the principles here apply to all QObjects. They apply to both C++ and QML code.

      QObjects do not contain threads. Rather, each object has a "thrad affinity". Please start by reading the "QObject|Thread Affinity":http://qt-project.org/doc/qt-5/qobject.html#thread-affinity article.

      I don't know the specifics of SDL events, but this is the general approach for polling:

      Make your polling function a slot of your wrapper QObject.

      Connect that slot to a QTimer's timeout() signal (see http://qt-project.org/doc/qt-5/QTimer.html )

      Run your timer at your desired polling interval. Then, every time the timer emits the timeout() signal, your polling slot will run.

      After you have finished processing your SDL events, emit the results in a signal. You will need to declare the signal in your wrapper QObject too.

      If you want to use a high polling rate, or the processing takes a long time, you might need to move your QObject to a separate thread.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0

      1/2

      4 May 2014, 00:35

      • Login

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