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. To SubClass or not to subclass? this is the question...
Qt 6.11 is out! See what's new in the release blog

To SubClass or not to subclass? this is the question...

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.2k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I was just wondering if its still a bad idea or lets say considered the wrong way to deal with threads if you subclass QThread I know in the 4.x versions any code i did with sub-classing of threads causes so much complaints that its the wrong way with other developers is this still the case with Qt5 I just feel that it is a QObject I mean there are signals going on right?
    what I wonder about is if it works with no performance hit whats the fuss about!
    and with regards to variables accessed from two different threads I can always lock and unlock threads so again I don't see why its wrong!

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

      Hi,

      Both approaches are now described in the latest documentation with pros and cons. So the only good answer is: it depends on your use case. However most of the times you don't need to subclass.

      The fuss is generally with thread affinity and where the slot will get executed (in the context of the thread managed by QThread or in the main thread ?) which are things that might be a bit convoluted when not knowing how direct/queued connections works and when and why they are chosen.

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

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        So what type of scenario would require that I don't go the subclass approach im sorry for asking this but its just one of the very few subjects with QT that im really confused with!

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          It all depends on your way of implementation. If you take care of where objects are created and wherer their slots are related to, you can subclass. I like that aproach more.

          What I do is create a QThread subclass, give it some slots, and forward them to an inner object, created in the run method. This is save.

          Or if you just use it as worker thraed without signal/slot (which is possible!), you subclass.

          Thats just my 2 cents on this.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

            [quote author="echostorm" date="1413812237"]So what type of scenario would require that I don't go the subclass approach im sorry for asking this but its just one of the very few subjects with QT that im really confused with![/quote]

            Usually, if you find yourself adding signals & slots to your subclassed QThread class, you should know that you are threading on thin ice and really need understand what you are doing.

            My rule of thumb: use a vanilla QThread with a worker object unless you:

            • are only doing some simple processing in your run() reimplementation that does not require signals and slots; or
            • know what you are are doing and really understand signals, slots, objects, and threading in their interactions.
            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              thanks for both of your inputs im just going to conclude that its not wrong to subclass hence im forwarding this post to my colleague developers and yes I think ill stick to subclassing cause its just much better for me since im used to that approach.

              Again Thanks very much!

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Thanks Andre I think what im doing is simple enough and basic lock unlock thread is all I require to make sure things run smoothly...

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

                  You probably already know it but avoid terminating your QThreads, a loop breaking variable is way nicer ;)

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

                  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