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. error: type 'QObject' is not a direct base of 'MyThread'
QtWS25 Last Chance

error: type 'QObject' is not a direct base of 'MyThread'

Scheduled Pinned Locked Moved Unsolved General and Desktop
threadqobjectmainwindow
11 Posts 3 Posters 3.2k 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.
  • D Offline
    D Offline
    deleted286
    wrote on 8 Jan 2021, 13:59 last edited by
    #1

    I have an error on the

    MyThread::MyThread(QObject* parent)
       : QObject(parent) ///Here is an error 
    {
       
    }
    
    

    Could someone tell me where I've gone wrong - thanks!

    mythread.cpp

    #include "mythread.h"
    #include "mainwindow.h"
    #include <QtCore>
    #include <QDebug>
    #include <QFile>
    #include <QTimer>
    #include <QThread>
    #include <QMutex>
    #include <QQueue>
    #include <QMessageBox>
    #include <QApplication>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QMainWindow>
    #include <QFileDialog>
    #include <QMainWindow>
    
    MyThread::MyThread(QObject* parent)
        : QObject(parent)
    {
        
    }
    
    MyThread::~MyThread() {
    
    }
    
    void MyThread::writeData()
    {
    
    ....
        emit writingDone();
    }
    
    
    void MyThread::run()  //Reading file from txt with thread1
    {
        ...
    }
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtWidgets/QMainWindow>
    #include <QMainWindow>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        ui->setupUi(this);
    
        // Initialize the new objects
        myThreadObject = new MyThread();
        myQThread = new QThread();
    
        // Move to new thread
        myThreadObject->moveToThread(myQThread);
    
        // connect signal and slots
        connect(this, &MainWindow::startWriting, myThreadObject, &MyThread::writeData);
        connect(myThreadObject, &MyThread::writingDone, this, &MainWindow::writingDoneByThread);
    
        // Start the new thread
        myQThread->start();
    }
    
    
    J 1 Reply Last reply 8 Jan 2021, 14:06
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 8 Jan 2021, 14:02 last edited by
      #2

      'QObject' is not a direct base of 'MyThread'

      And what do you don't understand here? The message explicitly tells you what's wrong. MyThread doesn't directly derive from QObject...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • D deleted286
        8 Jan 2021, 13:59

        I have an error on the

        MyThread::MyThread(QObject* parent)
           : QObject(parent) ///Here is an error 
        {
           
        }
        
        

        Could someone tell me where I've gone wrong - thanks!

        mythread.cpp

        #include "mythread.h"
        #include "mainwindow.h"
        #include <QtCore>
        #include <QDebug>
        #include <QFile>
        #include <QTimer>
        #include <QThread>
        #include <QMutex>
        #include <QQueue>
        #include <QMessageBox>
        #include <QApplication>
        #include <QtWidgets/QApplication>
        #include <QtWidgets/QMainWindow>
        #include <QFileDialog>
        #include <QMainWindow>
        
        MyThread::MyThread(QObject* parent)
            : QObject(parent)
        {
            
        }
        
        MyThread::~MyThread() {
        
        }
        
        void MyThread::writeData()
        {
        
        ....
            emit writingDone();
        }
        
        
        void MyThread::run()  //Reading file from txt with thread1
        {
            ...
        }
        

        mainwindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QtWidgets/QMainWindow>
        #include <QMainWindow>
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
        {
            ui->setupUi(this);
        
            // Initialize the new objects
            myThreadObject = new MyThread();
            myQThread = new QThread();
        
            // Move to new thread
            myThreadObject->moveToThread(myQThread);
        
            // connect signal and slots
            connect(this, &MainWindow::startWriting, myThreadObject, &MyThread::writeData);
            connect(myThreadObject, &MyThread::writingDone, this, &MainWindow::writingDoneByThread);
        
            // Start the new thread
            myQThread->start();
        }
        
        
        J Offline
        J Offline
        JonB
        wrote on 8 Jan 2021, 14:06 last edited by
        #3

        @suslucoder
        Show your declaration of class MyThread in mythread.h.

        D 1 Reply Last reply 8 Jan 2021, 14:11
        0
        • J JonB
          8 Jan 2021, 14:06

          @suslucoder
          Show your declaration of class MyThread in mythread.h.

          D Offline
          D Offline
          deleted286
          wrote on 8 Jan 2021, 14:11 last edited by
          #4

          @JonB

          #define MYTHREAD_H
          #include <QtCore>
          #include <QMainWindow>
          #include <QObject>
          #include <QWidget>
          #include <QQueue>
          #include <QMutex>
          
          class MyThread: public QThread
          {
              Q_OBJECT
          
          public:
              MyThread(QObject* parent =nullptr);
              ~MyThread();
          
          signals:
              void writingDone(); //signal for main thread
          
          public slots:
              void writeData(); // this slot ask the thread to write data
          
              void run();
          
          
          private:
              QQueue<double> queue;
          };
          
          #endif // MYTHREAD_H
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 8 Jan 2021, 14:12 last edited by
            #5

            @suslucoder said in error: type 'QObject' is not a direct base of 'MyThread':

            class MyThread: public QThread

            Exactly what the error message told you...
            No need to derive from QThread here at all btw.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            D 1 Reply Last reply 8 Jan 2021, 14:27
            0
            • C Christian Ehrlicher
              8 Jan 2021, 14:12

              @suslucoder said in error: type 'QObject' is not a direct base of 'MyThread':

              class MyThread: public QThread

              Exactly what the error message told you...
              No need to derive from QThread here at all btw.

              D Offline
              D Offline
              deleted286
              wrote on 8 Jan 2021, 14:27 last edited by
              #6

              @Christian-Ehrlicher Im trying to do subclass QObject. I can read also the error. Obviously I'm doing something wrong. Do you think is it the true way you make me realize my mistake?

              J 1 Reply Last reply 8 Jan 2021, 14:40
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 8 Jan 2021, 14:31 last edited by
                #7

                @suslucoder said in error: type 'QObject' is not a direct base of 'MyThread':

                Im trying to do subclass QObject

                But you don't:

                class MyThread: public QThread

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • D deleted286
                  8 Jan 2021, 14:27

                  @Christian-Ehrlicher Im trying to do subclass QObject. I can read also the error. Obviously I'm doing something wrong. Do you think is it the true way you make me realize my mistake?

                  J Offline
                  J Offline
                  JonB
                  wrote on 8 Jan 2021, 14:40 last edited by JonB 1 Aug 2021, 14:41
                  #8

                  @suslucoder
                  Can I ask you one thing: are you trying to use threads because this is an assignment/exam/educational? Do you really have any need for using threads, for whatever you are trying to achieve? Threads are one of the hardest things to get right. So many beginners on this forum start out trying to use threads when there is no need to do so, and run into problems, it could be a bad idea and not at all needed. If I were learning Qt they are the last thing I would want to start out on. Unless you have a good reason for really wanting to use them.

                  D 1 Reply Last reply 10 Jan 2021, 10:43
                  2
                  • J JonB
                    8 Jan 2021, 14:40

                    @suslucoder
                    Can I ask you one thing: are you trying to use threads because this is an assignment/exam/educational? Do you really have any need for using threads, for whatever you are trying to achieve? Threads are one of the hardest things to get right. So many beginners on this forum start out trying to use threads when there is no need to do so, and run into problems, it could be a bad idea and not at all needed. If I were learning Qt they are the last thing I would want to start out on. Unless you have a good reason for really wanting to use them.

                    D Offline
                    D Offline
                    deleted286
                    wrote on 10 Jan 2021, 10:43 last edited by
                    #9

                    @JonB It is not an assignment or an exam. I just want to learn all subjects quickly

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 10 Jan 2021, 10:58 last edited by
                      #10

                      @suslucoder said in error: type 'QObject' is not a direct base of 'MyThread':

                      I just want to learn all subjects quickly

                      Then I would start with the basics and not with advanced topics like threads.

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      D 1 Reply Last reply 10 Jan 2021, 11:20
                      0
                      • C Christian Ehrlicher
                        10 Jan 2021, 10:58

                        @suslucoder said in error: type 'QObject' is not a direct base of 'MyThread':

                        I just want to learn all subjects quickly

                        Then I would start with the basics and not with advanced topics like threads.

                        D Offline
                        D Offline
                        deleted286
                        wrote on 10 Jan 2021, 11:20 last edited by
                        #11

                        @Christian-Ehrlicher thank you, i understand my mistake and solve it.

                        1 Reply Last reply
                        0

                        6/11

                        8 Jan 2021, 14:27

                        • Login

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