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] Call virtual methods from QThread::run()

[SOLVED] Call virtual methods from QThread::run()

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.9k 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.
  • F Offline
    F Offline
    frankiefrank
    wrote on last edited by
    #1

    This may be a C++ question so apologies in advance - I just couldn't google an answer (or even a variation on the question).

    I have a class subclassing QThread, and it has a pure virtual method.

    @
    class Thread : public QThread
    {
    public:
    Thread(QObject *parent);
    virtual void run()
    {
    while (true) {
    doSomething();
    }
    }
    virtual void doSomething() = 0;
    };
    @

    I have a class subclassing the above, with an implementation of that virtual method
    @
    class ThreadSub : public Thread
    {
    ThreadSub(QObject *parent)
    : Thread(parent)
    {
    }

    virtual void doSomething() {
        qDebug() << "something";
    }
    

    };
    @

    I created an instance of ThreadSub and called start(). I got an error saying "Pure virtual method called".

    This is a simplified version of the code I had but, based on what is visible here, should there be any issue?

    "Roads? Where we're going, we don't need roads."

    1 Reply Last reply
    0
    • F Offline
      F Offline
      frankiefrank
      wrote on last edited by
      #2

      And a clarification - in my full code, the constructor or destructor of the base class do not the virtual method.

      "Roads? Where we're going, we don't need roads."

      1 Reply Last reply
      0
      • B Offline
        B Offline
        butterface
        wrote on last edited by
        #3

        I think it is the same problem as with the constructor. If you overwrite the run method again, it should work (but I did not try!).

        1 Reply Last reply
        0
        • F Offline
          F Offline
          frankiefrank
          wrote on last edited by
          #4

          Thanks for your comment butterface,

          But I do want my run() code to run in the base class - that's the whole point of my class-subclass separation.

          "Roads? Where we're going, we don't need roads."

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            The code you posted works fine, so there must be something wrong in the real code.
            It's usually a better idea to separate the thread management (QThread object) from the work itself (a QObject derived worker object). Deriving from QThread has all sorts of potential problems.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              frankiefrank
              wrote on last edited by
              #6

              I ended up changing the code a lot - but if I remember correctly, this WAS eventually an issue with the destructor/constructor. Wish I had more to add but anyway it's solved.

              "Roads? Where we're going, we don't need roads."

              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